Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: canonicalize is not supported on wasi target #124

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ jobs:
- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Install fixtures dependencies
working-directory: fixtures/pnpm8
run: pnpm install --frozen-lockfile

- name: Build
run: |
rustup target add wasm32-wasi-preview1-threads
Expand Down
32 changes: 30 additions & 2 deletions napi/__test__/resolver.spec.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { join, sep } from 'node:path'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'

import test from 'ava'
Expand Down Expand Up @@ -217,7 +217,7 @@ for (const [title, context, request, expected] of [
'handle fragment escaping',
enhancedResolveRoot,
'./no\0#fragment/\0#/\0##fragment',
join(enhancedResolveRoot, 'no#fragment','#', '#.js#fragment'),
join(enhancedResolveRoot, 'no#fragment', '#', '#.js#fragment'),
],
]) {
test(title, (t) => {
Expand All @@ -229,3 +229,31 @@ for (const [title, context, request, expected] of [
t.is(resolver.sync(context, request).path, expected)
})
}

test('resolve pnpm package', (t) => {
const pnpmProjectPath = join(currentDir, '..', '..', 'fixtures', 'pnpm8')
const resolver = new ResolverFactory({
aliasFields: ['browser'],
})
t.deepEqual(resolver.sync(pnpmProjectPath, 'styled-components'), {
path: join(
pnpmProjectPath,
'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'
),
})
t.deepEqual(
resolver.sync(
join(
pnpmProjectPath,
'node_modules/.pnpm/styled-components@6.1.1_react-dom@18.2.0_react@18.2.0/node_modules/styled-components'
),
'react'
),
{
path: join(
pnpmProjectPath,
'node_modules/.pnpm/react@18.2.0/node_modules/react/index.js'
),
}
)
})
28 changes: 27 additions & 1 deletion src/file_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,32 @@ impl FileSystem for FileSystemOs {
}

fn canonicalize(&self, path: &Path) -> io::Result<PathBuf> {
dunce::canonicalize(path)
#[cfg(not(target_os = "wasi"))]
{
dunce::canonicalize(path)
}
#[cfg(target_os = "wasi")]
{
let meta = fs::symlink_metadata(path)?;
if meta.file_type().is_symlink() {
let link = fs::read_link(path)?;
let mut path_buf = path.to_path_buf();
path_buf.pop();
for segment in link.iter() {
match segment.to_str() {
Some("..") => {
path_buf.pop();
}
Some(".") | None => {}
Some(seg) => {
path_buf.push(seg.trim_end_matches('\0'));
Boshen marked this conversation as resolved.
Show resolved Hide resolved
Boshen marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Ok(path_buf)
} else {
Ok(path.to_path_buf())
}
}
}
}