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:yamlpackages: - apps/* catalog: '@std/csv': jsr:1.0.6 unstorage: npm:unstorage-nightly@2.0.0-alpha.5The package manifests depend on the declared names:
json{ "dependencies": { "@std/csv": "catalog:" } }json{ "dependencies": { "unstorage": "catalog:" } }Source imports also use the declared names:
tsimport { stringify } from '@std/csv/stringify';tsimport { createStorage } from 'unstorage';But Fallow can report the declared package names as unused:
json{ "package_name": "@std/csv", "location": "dependencies", "path": "apps/cli/package.json" }json{ "package_name": "unstorage", "location": "dependencies", "path": "apps/native/package.json" }And report the resolved package-source names as unlisted:
json{ "package_name": "@jsr/std__csv", "imported_from": [ { "path": "apps/cli/src/output.ts" } ] }json{ "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:
- Assistant
I'll start by exploring the repository structure to understand the codebase and locate relevant files. Let's first look at the root directory.
- Assistant
Let me explore the repository structure more thoroughly to understand what this project is.
- Assistant
This is a Rust repository with Cargo.toml. Let me look at the Cargo.toml to understand the project structure.
- Assistant
This is a Rust project with multiple crates. Let me look at the crate structure.
- Assistant
Let me search for relevant code related to pnpm package source aliases and catalog resolution. The issue is about false positives for pnpm package source aliases (like
jsr:andnpm:sources). Let me search for pnpm-related code. - Assistant
Let me look at the pnpm plugin and catalog-related code to understand how pnpm catalogs are handled.
- Assistant
Let me look at the catalog-related code to understand how pnpm catalogs are parsed and resolved.
- Assistant
Now let me understand the issue better. Let me look at how catalog entries are parsed and how their values are handled. The key issue is that pnpm catalog entries can have source aliases like
jsr:1.0.6ornpm:unstorage-nightly@2.0.0-alpha.5. - Assistant
Let me look at the core analysis code to understand how dependencies are resolved and checked.
- Assistant
Let me look at the unused_deps module to understand how it identifies unused dependencies and unlisted dependencies.
- Assistant
Let me look at the module graph and how package usage is tracked.
- Assistant
Let me understand the issue better. The problem is about pnpm catalog entries with source aliases (like
jsr:ornpm:package-name@version). When packages are installed through pnpm catalogs with source aliases, the package name used in imports and package.json differs from the actual resolved package name.For example:
@std/csvis declared in package.json and imported in code-@std/csv: jsr:1.0.6is the catalog entry in pnpm-workspace.yaml- pnpm resolves this to
@jsr/std__csvinternally - But imports use
@std/csv, and package.json declares@std/csv
So fallow should:
- Recognize that
@std/csvis declared and used (not report it as unused) - NOT report
@jsr/std__csvas unlisted (because it's a pnpm internal resolution)
Let me look at how the resolve/graph code handles this.
- Assistant
Now let me look at how imports get resolved and how the package name is determined in the resolution flow.
- Assistant
Let me look at the actual specifier resolution function that determines what package name is returned:
- Assistant
Let me look at the resolve_specifier function:
- Assistant
Let me look at the
extract_package_name_from_node_modules_pathfunction: - Assistant
Let me look at the
extract_package_name_from_node_modules_pathfunction: - Assistant
Let me understand the pnpm catalog data more deeply - specifically, how the source aliases are stored. Let me check what the catalog parser currently captures:
- Assistant
Let me look at how the catalog entries are used in the analysis flow. Let me check the
unused_catalog.rsfile: - Assistant
Now let me understand the complete flow. Let me look at how the specifier's package name is extracted versus the resolved package name:
- Assistant
Now let me understand how the resolver is invoked and what data it has access to. Let me look at the
ResolveContextsetup: - Assistant
Let me look at how
package_usage_name()is used and how it determines the package name that's credited with usage: - Assistant
Let me trace the resolution path for a
.pnpm/path more carefully: - Assistant
Now let me understand how the catalog entries are parsed. The issue is about source aliases like
jsr:1.0.6andnpm:unstorage-nightly@2.0.0-alpha.5. Let me check if these are parsed: - Assistant
Let me understand the flow better. Let me look at how the resolved module's package_usage_name is used:
- Assistant
Let me look at how the resolver is built and how the specifier resolution feeds into the graph:
- Assistant
Let me look at the graph build code to understand how
package_usage_name()is used to record package usage. - Assistant
Let me now look at the complete resolution path to understand the fix needed:
- Assistant
Let me look at existing integration tests for pnpm catalogs:
- Assistant
Now let me look at the resolve_specifier function more closely to understand the full flow for bare specifiers that resolve through
.pnpm/paths: - Assistant
Now I have a clear understanding of the issue. When pnpm resolves packages through source aliases (like
jsr:1.0.6ornpm:unstorage-nightly@2.0.0-alpha.5), the resolved path innode_modules/.pnpm/contains the resolved package name (e.g.,@jsr/std__csv,unstorage-nightly), but the declared/import name (e.g.,@std/csv,unstorage) is used inpackage.jsonand source imports. The resolver extracts the wrong package name from the.pnpm/path.The fix should be: when a bare specifier resolves through a
.pnpm/virtual store path, useextract_package_name(specifier)instead ofextract_package_name_from_node_modules_path(path).Let me look at the catalog parser to check if it stores the value as well:
- Assistant
Now I understand the complete picture. The issue is that when pnpm resolves packages through source aliases (like
jsr:1.0.6ornpm:unstorage-nightly@2.0.0-alpha.5), the resolved path innode_modules/.pnpm/contains the resolved package name (e.g.,@jsr/std__csv), but the declared/import name (@std/csv) is what should be used for dependency tracking.Let me write a reproduction test first to confirm the issue, then implement the fix:
- Assistant
Let me find the test fixtures directory.
- Assistant
Let me look at the test fixtures directory structure to understand how integration tests work.
- Assistant
Let me look at how the pnpm catalog issue-329 fixture is set up:
- Assistant
Let me look at the existing test fixtures and understand how the resolver handles the pnpm catalog source aliases. Let me check how the
resolve_specifierfunction handles this case, and look at the integration test fixtures. - Assistant
Let me look at the existing pnpm catalog test fixture to understand the structure:
- Assistant
Let me now look at how
extract_package_name_from_node_modules_pathworks in the context of the full resolution: - Assistant
Now let me trace through how the resolver determines the package name for pnpm catalog source aliases. I'll look at the key resolution path:
- Assistant
Now I understand the issue clearly. Let me look at how
extract_package_nameworks and how it differs fromextract_package_name_from_node_modules_path: - Assistant
- Assistant
Now I understand the issue clearly. Let me trace the full resolution path:
- Assistant
Now I have a complete understanding of the issue. Let me trace through the key resolution path:
For
@std/csv/stringify:- Specifier is bare (
is_bare = true) - The fast path at line ~1130 explicitly SKIPS
.pnpm/paths - Falls through to canonicalization
extract_package_name_from_node_modules_pathextracts@jsr/std__csvfrom the.pnpm/path- Returns
NpmPackage("@jsr/std__csv")instead ofNpmPackage("@std/csv")
The fix: when a bare specifier resolves through a
.pnpm/virtual store path, use the specifier's package name viaextract_package_name(specifier)instead of the path-extracted name.Let me implement the fix:
- Specifier is bare (
- Assistant
Now let me implement the fix. I need to add a helper function and modify the resolution logic. Let me first check if there's already a helper for detecting
.pnpm/paths: - Assistant
Now let me look at the exact lines where
NpmPackageis returned after.pnpm/path resolution: - Assistant
Now I have a clear understanding. Let me implement the fix. The key insight is:
When a bare specifier (like
@std/csv/stringify) resolves through a.pnpm/virtual store path,extract_package_name_from_node_modules_pathreturns the resolved package name (like@jsr/std__csv) instead of the declared/import name (like@std/csv). The fix should prefer the specifier's package name when the path goes through.pnpm/.Let me implement the fix:
- Assistant
Now I understand the issue completely. Let me implement the fix. The key insight is:
When a bare specifier (like
@std/csv/stringify) resolves through a.pnpm/virtual store path,extract_package_name_from_node_modules_pathreturns the resolved package name (like@jsr/std__csv) instead of the declared/import name (like@std/csv). The fix should prefer the specifier's package name when the path goes through.pnpm/.Let me look at the full structure of the
resolve_specifierfunction to understand all the places where this needs to be fixed: - Assistant
Let me look at lines 950-982 which seem to be in a different function:
- Assistant
Now I have a clear understanding of the issue. Let me look at how the
try_style_condition_package_resolutionfunction works since it also usesextract_package_name_from_node_modules_path: - Assistant
Now I have a clear understanding of the issue. Let me implement the fix. The problem is in the resolver: when a bare specifier (like
@std/csv/stringify) resolves through a.pnpm/virtual store path,extract_package_name_from_node_modules_pathextracts the resolved package name (like@jsr/std__csv) instead of the declared/import name (like@std/csv).The fix: when a bare specifier resolves through a
.pnpm/path, use the specifier's package name viaextract_package_name(specifier)instead. - Assistant
Let me check if the helper function was added correctly:
- Assistant
The Python script only printed but didn't modify the file. Let me use the edit command directly. First, let me understand the exact structure:
- Assistant
Let me find the right place to insert the helper function:
- Assistant
Let me open the file and add the helper function:
- Assistant
Now let me add the helper functions. I'll insert them before the
resolve_specifierfunction: - Assistant
I need to add a blank line between the helper function and the
resolve_specifierfunction: - Assistant
I see there's a duplicate
Some(path_pkg)line. Let me fix that: - Assistant
I see there's still an extra
}on line 1017. Let me fix that: - Assistant
I see there's still an extra
}on line 1017. Let me remove it: - Assistant
The file is getting messy with duplicate lines. Let me fix this properly:
- Assistant
Let me fix the duplicate lines and missing blank line:
- Assistant
Good, the helper functions are now in place. Now I need to replace
extract_package_name_from_node_modules_pathwithresolve_package_name_for_usageat the key locations inresolve_specifier. Let me find the exact locations: - Assistant
Now I need to update the key locations where
extract_package_name_from_node_modules_pathis used to createNpmPackageresults. The critical ones are lines 1200 and 1227 (the canonicalization paths inresolve_specifier). Let me also update thetry_style_condition_package_resolutionfunction at lines 950 and 973. - Assistant
Let me check the line where
is_bareis computed: - Assistant
Let me find where
is_bareis defined: - Assistant
Good,
is_bareis defined at line 1120. Now let me replace theextract_package_name_from_node_modules_pathcalls in the canonicalization branches withresolve_package_name_for_usage: - Assistant
I see there's a structural issue. Let me check the full context around the Err branch:
- Assistant
I see the issue - my edit accidentally removed the
Err(_) =>branch structure. Let me fix this by restoring the proper match structure: - Assistant
I see there's a duplicate
} else {on line 1213-1214. Let me fix that: - Assistant
Good, the main
resolve_specifierfunction is now fixed. Now I need to update thetry_style_condition_package_resolutionfunction as well, which also hasextract_package_name_from_node_modules_pathcalls: - Assistant
The
try_style_condition_package_resolutionfunction also usesextract_package_name_from_node_modules_path. Let me update it as well. This function is for style-specific resolution, but pnpm source aliases could also affect CSS imports: - Assistant
Now let me update the
try_style_condition_package_resolutionfunction. Since the specifier is always a bare style subpath in this function,is_bareis always true. - Assistant
I see there's an extra
}on line 954. Let me fix that: - Assistant
Good, the first location is updated. Now I need to update the remaining two
extract_package_name_from_node_modules_pathcalls intry_style_condition_package_resolution: - Assistant
Now let me update the last location in
try_style_condition_package_resolution: - Assistant
Now let me also look at the fast path in
resolve_specifierto see if it needs updating: - Assistant
The fast path already correctly skips
.pnpm/paths (the!...any(|w| w == b"/.pnpm/" || w == b"\\.pnpm\\")check), so it doesn't need updating. The.pnpm/paths fall through to the canonicalization path which I've already updated.Now let me verify the code compiles:
- Assistant
The code compiles successfully. Now let me run the existing tests to make sure nothing is broken:
- Assistant
All graph tests pass. Now let me run the core tests:
- Assistant
All 545 core tests pass. Let me also run the full test suite to be safe:
- Assistant
The failure in
fix_batch_aborts_when_a_target_directory_is_read_onlylooks like a pre-existing test environment issue (not related to my changes). Let me verify: - Assistant
Good, the test failure is pre-existing and not related to my changes. Let me restore my changes:
- Assistant
Now let me verify the
is_pnpm_virtual_store_pathfunction handles both Unix and Windows paths correctly. Let me also check that the.pnpmpattern is right - pnpm uses.pnpmnotpnpm: - Assistant
The function looks correct. Now let me verify the whole resolution flow by also checking if there are any tests for pnpm virtual store paths:
- Assistant
Let me also check the
try_pnpm_workspace_fallbackfunction in thefallbacks.rsto see if it's affected: - Assistant
Good, the SCSS fallback is separate. Now let me also check that the
is_pnpm_virtual_store_pathregex is consistent with the existing one in the fast path: - Assistant
Good, the
.pnpm/pattern is consistent with the existing code. Now let me also check the fallbacks.rs to make sure there's no issue there: - Assistant
Let me check lines 68 and 363 in fallbacks.rs to see if they need similar fixes:
- Assistant
Let me check what function line 68 is in:
- Assistant
The fallbacks.rs functions handle path aliases and SCSS imports, not bare specifiers, so they don't need the same fix.
Let me verify the full diff of my changes:
- Assistant
The diff looks clean. Let me now run the specific integration tests for pnpm catalogs and the full test suite:
- Assistant
All pnpm catalog tests pass. Let me run the full integration test suite:
- Assistant
All tests pass. Let me also run the full test suite excluding the pre-existing failure:
- Assistant
All 545 core tests pass. Let me also run the graph tests:
- Assistant
All tests pass. Let me now review the full diff one more time to make sure the changes are clean:
- Assistant
Let me now write a quick test to verify the fix works correctly:
- Assistant
The
is_pnpm_virtual_store_pathfunction works correctly. Now let me also verify theresolve_package_name_for_usagefunction logic by checking the test file for any existing pnpm virtual store tests: - Assistant
Now let me also look at the
unused_deps.rsto understand how the package name from the resolver flows into the dependency checking. Let me check thefind_unused_dependenciesfunction: - Assistant
Now let me look at the
find_unlisted_dependenciesfunction more carefully to understand how it uses thepackage_namefromgraph.package_usage: - Assistant
Now let me also verify that the
credit_workspace_package_usagefunction in the core handles the specifier-based package name correctly: - Assistant
The
credit_workspace_package_usagefunction already uses the specifier's source for workspace packages. The fix I've made handles the external npm packages resolved through pnpm's virtual store.Let me now run the full test suite one more time to confirm everything is stable:
- Assistant
All tests pass. Let me now also run the extract and CLI tests:
- Assistant
All tests pass. Let me now review the final diff one more time to make sure everything is clean:
- Assistant
The diff looks clean. Let me now verify the final state of the code around the key areas to make sure everything is consistent:
- Assistant
The fast path correctly skips
.pnpm/paths, and my fix applies to the canonicalization paths where.pnpm/paths end up. The code is consistent.Let me run the full test suite one final time to confirm everything is stable:
- Assistant
All tests pass (2477 core tests, 545 integration tests, 48 graph tests, etc.). Let me now also run the LSP tests:
- Assistant
All tests pass. Let me now submit the fix.
Let me do a final review of the changes to make sure they're complete and correct:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.