ISSUE DESCRIPTION False positives for pnpm package source aliases What happened? fallow…
- User
ISSUE DESCRIPTION
False positives for pnpm package source aliases
What happened?
fallow dead-codereports incorrect unused/unlisted dependency findings when pnpm package sources are used through catalogs.Example
pnpm-workspace.yaml:packages: - apps/* catalog: '@std/csv': jsr:1.0.6 unstorage: npm:unstorage-nightly@2.0.0-alpha.5The package manifests depend on the declared names:
{ "dependencies": { "@std/csv": "catalog:" } }{ "dependencies": { "unstorage": "catalog:" } }Source imports also use the declared names:
import { stringify } from '@std/csv/stringify';import { createStorage } from 'unstorage';But Fallow can report the declared package names as unused:
{ "package_name": "@std/csv", "location": "dependencies", "path": "apps/cli/package.json" }{ "package_name": "unstorage", "location": "dependencies", "path": "apps/native/package.json" }And report the resolved package-source names as unlisted:
{ "package_name": "@jsr/std__csv", "imported_from": [ { "path": "apps/cli/src/output.ts" } ] }{ "package_name": "unstorage-nightly", "imported_from": [ { "path": "apps/native/src/storage.ts" } ] }This seems to conflate the manifest/import package name with pnpm’s resolved package-source name.
pnpm documents that packages can be installed from alternate sources:
jsr:installs from the JSR registrynpm:<name>@<version>can resolve a declared dependency through a different npm package name- package sources are part of pnpm’s supported dependency model, not separate source imports
So if
@std/csvis declared and imported, Fallow should credit@std/csv, even if pnpm resolves it to@jsr/std__csvinternally. Same forunstorageresolving tounstorage-nightly.Reproduction
Create
pnpm-workspace.yaml: Assistant
Let me understand the issue:
-
pnpm has a feature called "package sources" or "catalogs" where you can declare dependencies in
pnpm-workspace.yamlwith alternate sources likejsr:ornpm:<different-name>@<version>. -
For example:
@std/csv: jsr:1.0.6- means@std/csvis resolved from JSR registryunstorage: npm:unstorage-nightly@2.0.0-alpha.5- meansunstorageis resolved to a different npm packageunstorage-nightly
-
In
package.json, the dependency is listed as"@std/csv": "catalog:"and"unstorage": "catalog:". -
The imports in TypeScript use the declared names:
import { stringify } from '@std/csv/stringify'andimport { createStorage } from 'unstorage'. -
Fallow (dead-code detector) is incorrectly:
- Reporting
@std/csvandunstorageas unused (because it's matching against the resolved names@jsr/std__csvand )
- Reporting
-
Assistant
This is a Rust project. Let me explore the crates directory to understand the structure better.
Assistant
Let me search for relevant code related to pnpm, package sources, catalogs, and dependency resolution.
Assistant
Let me start by looking at the existing test files for pnpm catalog issues and the relevant source files
Assistant
Let me look at the pnpm catalog module and related resolution code to understand how package sources are resolved.
Assistant
Now let me look at the resolver and specifier modules:
Assistant
Let me search for key patterns related to the issue.
Assistant
Let me look at the resolution of npm packages - specifically how they match against package.json dependencies
Assistant
Let me look at the key resolution types and how dependency matching works
Assistant
Let me look at the resolution types and how dependency matching works.
Assistant
Now let me look at the main resolve function and the fallbacks module to understand how npm packages are resolved
Assistant
Let me look at the unused_deps analysis code to understand the matching logic
Assistant
Let me look at the key analysis files
Assistant
Let me understand the resolution flow better. Let me look at how
package_usageis builtAssistant
Let me look at the core lib.rs and graph build to understand how package usage is
Assistant
Let me look at the graph build module where package usage is first recorded:
Assistant
Let me search for how jsr or pnpm package source aliasing works in the code
Assistant
Let me look at the specifier resolution code more closely, specifically how bare specifiers are resolved to NpmPackage
Assistant
Let me look at the path_info module:
Assistant
Let me look at the critical part of the specifier resolution where NpmPackage is returned:
Assistant
Let me look at the
extract_package_name_from_node_modules_pathfunction and the surrounding resolution logicAssistant
Let me view the
extract_package_name_from_node_modules_pathfunction and surrounding context:Assistant
Let me look at the resolution of bare specifiers more carefully in the specifier module
Assistant
Let me look at the successful resolution handling earlier in the specifier module
Assistant
Let me look at some test fixtures to understand the catalog integration:
Assistant
Let me look at the issue-329 catalog fixture to understand the setup and pattern:
Assistant
Let me look at the full resolve_specifier function:
Assistant
Now I have a clear understanding of the issue. Let me search for all places where
NpmPackageis created:Assistant
Now I have a thorough understanding. Let me look at the fallbacks that need changes too
Assistant
Let me look at the
try_workspace_package_fallbackfunction andtry_package_imports_fallbackAssistant
Now I have a clear picture. Let me create a reproduction script first to understand the exact problem, then implement the fix.
Let me first look at how the existing tests for the specifier work:
Assistant
Now I understand the full issue. Let me implement the fix. The core problem is that when the oxc_resolver resolves a bare specifier through node_modules, it may follow symlinks to directories with different names (due to pnpm package source aliases like
jsr:ornpm:<other-name>). Theextract_package_name_from_node_modules_paththen returns the "real" directory name rather than the declared package name.The fix: when creating
NpmPackagefrom a node_modules path for a bare specifier, compare with the specifier's extracted package name and prefer the specifier's name when they differ.Let me first look at the import statements in specifier.rs to understand what's already imported:
Assistant
Now let me implement the fix. I'll modify the key places in
resolve_specifierandtry_style_condition_package_resolutioninspecifier.rs.Let me first look at the specific lines where the first change needs to happen (the early bare check):
Assistant
Let me look at lines 1150-1230 to see the canonical and non-canonical paths
Assistant
Now I'll implement the fix. Let me start by modifying the
specifier.rsfile:Assistant
Let me first add a helper function. I'll add it near the top of the resolution functions in specifier.rs
Assistant
Let me try a different syntax with the edit command
Assistant
Let me use replace to add the helper function before
is_node_modules_pathAssistant
Let me use goto to navigate there first
Assistant
Now let me apply the edit:
Assistant
Now let me modify
try_style_condition_package_resolutionto use the new helper. Let me scroll to see the relevant partAssistant
Let me now modify the three locations in
try_style_condition_package_resolution. First, lines 965-968Assistant
I see there's an extra closing brace. Let me fix that
Assistant
Now let me fix the second location (canonical path) in
try_style_condition_package_resolutionAssistant
Now fix the third location (the map pattern) in
try_style_condition_package_resolutionAssistant
Now let me fix the main
resolve_specifierfunction. Let me find the relevant locationsAssistant
Let me look at lines 1145-1165 (the early bare check)
Assistant
Now let me fix the first location in
resolve_specifier- the early bareAssistant
I need to handle the case where the path doesn't contain node_modules. Let me fix the canonical
Assistant
Now fix the third location (non-canonical path) in
resolve_specifierAssistant
Now let me also fix the
fallbacks.rsfile. Let me check the locations that need fixingAssistant
Let me look at the context around line 363 in fallbacks.rs
Assistant
Let me open fallbacks.rs
Assistant
Now let me verify the changes compile. Let me first check the full modified specifier.rs to make sure the logic is correct
Assistant
Let me check if the build succeeds while waiting
Assistant
Now let me also fix the
try_scss_node_modules_fallbackin fallbacks.rs. Let me look at lineAssistant
The
try_scss_node_modules_fallbackat line 335 has access tospecifier. But the specifier here is a SCSS import, which could be a bare package name likebourbonor a path like../variables. The specifier is stripped of./prefix to getbare, and thenfind_scss_in_node_modulesfinds the actual file.I need to fix this function too. But I need to import
extract_package_namein the fallbacks.rs if it's not already imported.Let me check what's imported in fallbacks.rs:
Assistant
Let me fix
try_scss_node_modules_fallbackin fallbacks.rsAssistant
Now let me fix
try_scss_node_modules_fallback. The fix should check ifbarediffers frompkg_name(the package name extracted from node_modules path). If so, usebareas the package name (or the package name part ofbare). Butbareis the full SCSS import path, not just the package name. For example,barecould bebootstrap/scss/functions. We need to extract the package name frombare.Let me use
extract_package_name(bare)to get the declared package name and compare it withpkg_namefrom node_modules path.Assistant
Let me verify the build again
Assistant
The build succeeds. Now let me run the relevant tests to make sure nothing is broken
Assistant
All tests pass. Now let me also run the integration tests related to pnpm catalog and unused deps
Assistant
Let me run all the integration tests for unused deps and unlisted de
Assistant
All 545 integration tests pass. Let me also run the specifier-specific tests
Assistant
Let me verify the logic is safe. There's a subtle edge case: what if the resolved path doesn't contain
node_modulesat all? Let me add a safety check to the early bare check:Assistant
Let me switch back to specifier
Assistant
Let me look at the early bare check to see if we need a safety guard. Let me revisit lines 1147-1166:
Assistant
I notice the early bare check lost the original guard that the path contains
node_modules. Let me add it back using `is_node_modulesAssistant
I need to also fix the third location in
try_style_condition_package_resolutionwhich lost the fallback toExternalFile. Let me restore that behavior:Assistant
I need to fix all three locations in
try_style_condition_package_resolutionto properly guard against non-node_modules paths. Let me fix them:Assistant
I see there's leftover dead code at lines 1005-1006. Let me remove it
Assistant
There's an extra closing brace. Let me remove
Assistant
Let me verify the build again now
Assistant
Build succeeds. Let me run all the tests again to make sure everything still passes
Assistant
Let me run the full integration tests one more time to make sure everything passes
Assistant
All 545 integration tests pass! Let me also run the broader test suite
Assistant
Let me check the complete test summary:
Assistant
All tests pass. Let me review the complete changes I've made using
git diffAssistant
The diff looks clean. Let me also run clippy to check for any lint
Assistant
No clippy warnings. Let me verify once more by reviewing the full diff one more time
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.