Skip to content

Commit e84f3fa

Browse files
committed
fix: canonicalize is not supported on wasi target
1 parent 42fc05f commit e84f3fa

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

napi/__test__/resolver.spec.mjs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { join, sep } from 'node:path'
1+
import path, { join } from 'node:path'
22
import { fileURLToPath } from 'node:url'
33

44
import test from 'ava'
@@ -217,7 +217,7 @@ for (const [title, context, request, expected] of [
217217
'handle fragment escaping',
218218
enhancedResolveRoot,
219219
'./no\0#fragment/\0#/\0##fragment',
220-
join(enhancedResolveRoot, 'no#fragment','#', '#.js#fragment'),
220+
join(enhancedResolveRoot, 'no#fragment', '#', '#.js#fragment'),
221221
],
222222
]) {
223223
test(title, (t) => {
@@ -229,3 +229,31 @@ for (const [title, context, request, expected] of [
229229
t.is(resolver.sync(context, request).path, expected)
230230
})
231231
}
232+
233+
test('resolve pnpm package', (t) => {
234+
const pnpmProjectPath = join(currentDir, '..', '..', 'fixtures', 'pnpm8')
235+
const resolver = new ResolverFactory({
236+
aliasFields: ['browser'],
237+
})
238+
t.deepEqual(resolver.sync(pnpmProjectPath, 'styled-components'), {
239+
path: join(
240+
pnpmProjectPath,
241+
'node_modules/.pnpm/styled-components@6.1.1_react-dom@18.2.0_react@18.2.0/node_modules/styled-components/dist/styled-components.browser.cjs.js'
242+
),
243+
})
244+
t.deepEqual(
245+
resolver.sync(
246+
join(
247+
pnpmProjectPath,
248+
'node_modules/.pnpm/styled-components@6.1.1_react-dom@18.2.0_react@18.2.0/node_modules/styled-components'
249+
),
250+
'react'
251+
),
252+
{
253+
path: join(
254+
pnpmProjectPath,
255+
'node_modules/.pnpm/react@18.2.0/node_modules/react/index.js'
256+
),
257+
}
258+
)
259+
})

src/file_system.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,32 @@ impl FileSystem for FileSystemOs {
9191
}
9292

9393
fn canonicalize(&self, path: &Path) -> io::Result<PathBuf> {
94-
dunce::canonicalize(path)
94+
#[cfg(not(target_os = "wasi"))]
95+
{
96+
dunce::canonicalize(path)
97+
}
98+
#[cfg(target_os = "wasi")]
99+
{
100+
let meta = fs::symlink_metadata(path)?;
101+
if meta.file_type().is_symlink() {
102+
let link = fs::read_link(path)?;
103+
let mut path_buf = path.to_path_buf();
104+
path_buf.pop();
105+
for segment in link.iter() {
106+
match segment.to_str() {
107+
Some("..") => {
108+
path_buf.pop();
109+
}
110+
Some(".") | None => {}
111+
Some(seg) => {
112+
path_buf.push(seg.trim_end_matches('\0'));
113+
}
114+
}
115+
}
116+
Ok(path_buf)
117+
} else {
118+
Ok(path.to_path_buf())
119+
}
120+
}
95121
}
96122
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy