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
I'll start by exploring the repository structure to understand what we're dealing with. Let me first check what's in the current directory.
- Assistant
Let me explore the repository structure more.
- Assistant
This is a Rust repository (Cargo.toml present). Let me understand the structure better.
- Assistant
Let me look at the issue more carefully. The issue is about pnpm package source aliases. When packages are declared with
catalog:references that point to alternate sources likejsr:ornpm:<name>@<version>, Fallow incorrectly:- Reports the declared package name as unused (e.g.,
@std/csv) - Reports the resolved package source name as unlisted (e.g.,
@jsr/std__csv)
Let me look for the relevant code that handles dependency resolution and the dead-code analysis.
- Reports the declared package name as unused (e.g.,
- Assistant
Let me search for "pnpm" and "source" related code to understand how pnpm package sources are handled.
- Assistant
The issue is about pnpm package source aliases. Let me look at the pnpm catalog handling code and how dependencies are resolved.
- Assistant
Now let me understand how the catalog entries are resolved. Let me look at the workspace parsers and how catalog values are processed.
- Assistant
Let me look at the workspace module and see how package.json dependencies are parsed, particularly focusing on how catalog: entries are resolved.
- Assistant
Let me look at how dependencies are collected from package.json, especially catalog references and source aliases.
- Assistant
Let me look at the existing test for pnpm catalog issue and the main analyze module.
- Assistant
Let me look at the core analysis code to understand how catalog entries are handled and how unused dependencies are detected.
- Assistant
Let me look at how the catalog entries are used in the dead-code analysis and how package names are resolved.
- Assistant
Let me search for where pnpm catalog values are resolved, especially for jsr: and npm: prefixed values.
- Assistant
Let me search for how pnpm's
node_modulesor lockfile is handled. - Assistant
Let me look at the unused_deps.rs file to understand how the dependency matching works.
- Assistant
Now I need to understand how the
graph.package_usageis built and how package names are resolved. Let me look at the module graph structure. - Assistant
Let me look at how the module graph resolves packages and how the pnpm catalog is used for resolution.
- Assistant
Let me look at how package resolution works and how pnpm catalog entries are resolved. Let me search for "pnpm" in the resolve module.
- Assistant
Let me look at the specifier resolution to understand how pnpm resolves package names.
- Assistant
Let me look at how
extract_package_name_from_node_modules_pathworks and how package names are resolved from node_modules paths. - Assistant
Let me look at the
extract_package_name_from_node_modules_pathfunction. - Assistant
Let me understand the flow better. I need to look at how the import specifier resolves and how
package_usage_nameworks. - Assistant
Let me look at the full resolve types to understand how imports are resolved.
- Assistant
Let me understand how
record_graph_package_usageworks and how it handles pnpm catalog aliases. - Assistant
Let me look at where imports are resolved and how they become
NpmPackageresults. - Assistant
Let me look at how pnpm overrides handle aliases to understand the pattern.
- Assistant
Let me look more closely at how bare specifiers are resolved, especially how pnpm catalog source aliases (like
jsr:1.0.6ornpm:unstorage-nightly@2.0.0-alpha.5) are handled. - Assistant
Let me look at how the main analysis function processes catalog data and how
credit_workspace_package_usageworks. - Assistant
Let me look at the main analysis function in lib.rs to see how it works.
- Assistant
Let me look at how the resolver handles bare specifiers and resolves them through node_modules.
- Assistant
Now I understand the problem. When pnpm resolves source aliases, the resolved package name in node_modules differs from the declared/import name. Let me trace the full flow.
The key issue is:
@std/csvis declared in package.json withcatalog:protocol- pnpm catalog defines
'@std/csv': jsr:1.0.6which resolves to@jsr/std__csvin node_modules - When resolving
import { stringify } from '@std/csv/stringify', it resolves to@jsr/std__csvin node_modules extract_package_name_from_node_modules_pathreturns@jsr/std__csv- So the import usage is recorded as
@jsr/std__csv, not@std/csv - This causes
@std/csvto be reported as unused and@jsr/std__csvas unlisted
Let me look at the
resolve_specifierfunction more carefully to understand the flow better. - Assistant
Let me look at the full resolve_specifier function, particularly the bare specifier handling path.
- Assistant
Now I understand the issue. Let me look at how the specifier's package name is extracted from a bare specifier.
- Assistant
Let me look at how pnpm handles the
.pnpmpath resolution to understand where the package name mismatch occurs. - Assistant
Let me look at
extract_package_nameto understand how it extracts the package name from a bare specifier. - Assistant
Now I have a clear understanding of the issue. Let me look at the test fixtures to understand the test structure better before I make my fix.
- Assistant
Let me find the fixtures directory.
- Assistant
Let me look at the issue-329 fixture to understand how it's structured, and then look at the issue_334 test.
- Assistant
Let me understand the issue more deeply by looking at how pnpm catalog entries with source aliases are parsed.
- Assistant
Now I understand the core issue. Let me trace through the resolution flow:
- Import specifier:
@std/csv/stringify(bare specifier) extract_package_name("@std/csv/stringify")returns"@std/csv"- pnpm resolves through catalog alias
jsr:1.0.6to@jsr/std__csvin node_modules extract_package_name_from_node_modules_path(resolved_path)returns"@jsr/std__csv"- Returns
ResolveResult::NpmPackage("@jsr/std__csv")instead ofResolveResult::NpmPackage("@std/csv")
The fix should be: when resolving a bare specifier through node_modules, if the resolved package name differs from the specifier's package name, use the specifier's name instead. Let me look at how to fix this.
Let me first look at the main analysis loop to understand where package_usage is built.
- Import specifier:
- Assistant
Let me look at the main lib.rs where
record_graph_package_usageis called and understand how import resolution flows into package usage tracking. - Assistant
Let me look at how the main analysis flow builds
package_usagefrom resolved imports. - Assistant
Let me look for where resolved imports contribute to
package_usage- there must be a place whereNpmPackageresults are turned into usage records. - Assistant
Let me search for where NpmPackage results contribute to package_usage.
- Assistant
Let me search more broadly for where NpmPackage results are turned into package usage records.
- Assistant
Let me search for where the graph's package_usage is populated from resolved imports.
- Assistant
Now I can see the key location. Let me look at
build.rswherepackage_usage_name()is used. - Assistant
Now I understand the issue completely. Let me trace the resolution:
- Import
@std/csv/stringifyis a bare specifier extract_package_name("@std/csv/stringify")returns@std/csv- pnpm resolves through
.pnpm/@jsr+std__csv@1.0.6/node_modules/@jsr/std__csv/stringify.js extract_package_name_from_node_modules_pathreturns@jsr/std__csvResolveResult::NpmPackage("@jsr/std__csv")is stored in package_usage
The fix should be: when we have a bare specifier that resolves to a different package name in node_modules, prefer the specifier's package name.
Let me first check if the project builds and passes tests before making changes.
- Import
- Assistant
Let me look at the resolve_specifier function more carefully to understand all the paths where NpmPackage is returned.
- Assistant
Now I have a clear picture of the issue. The problem is that when pnpm catalog entries use source aliases (
jsr:1.0.6,npm:unstorage-nightly@2.0.0-alpha.5), the resolved package name innode_modulesdiffers from the declared/import name. The resolver extracts the resolved name from the path, but the user's intent (and their package.json declaration) uses the declared name.Let me now understand the full flow to implement the fix. Let me look at the resolve_specifier function to see all NpmPackage return paths.
- Assistant
Now I understand the full picture. The fix should be: when resolving a bare specifier, if the resolved path in node_modules has a different package name than the specifier's package name, prefer the specifier's package name. This way
@std/csv(the declared/import name) is used instead of@jsr/std__csv(the resolved source name).Let me look at the pnpm catalog parsing more carefully to understand how the catalog entries with source aliases are stored.
- Assistant
So the current catalog parser just stores the package name and line number. It doesn't store the value at all. The value (like
jsr:1.0.6ornpm:unstorage-nightly@2.0.0-alpha.5) is simply ignored. The catalog entries are stored as(package_name, line).Let me look at the analyze flow to understand where the "unused" and "unlisted" reports come from.
- Assistant
Let me look at the main analysis function to understand how unused dependencies are detected.
- Assistant
Let me now understand exactly how the resolver resolves bare specifiers and where the package name mismatch happens. Let me trace the flow more carefully.
- Assistant
Now I have a clear understanding of the issue. Let me trace the key code path:
When
resolve_specifierprocesses a bare specifier like@std/csv/stringify:- The oxc resolver follows pnpm's symlinks/aliases and resolves to
@jsr/std__csvin node_modules extract_package_name_from_node_modules_pathreturns@jsr/std__csvfrom the resolved pathNpmPackage("@jsr/std__csv")is stored as the package usage name- But
@std/csvis what's in package.json dependencies
The fix: for bare specifiers, when returning
NpmPackage, useextract_package_name(specifier)(the declared name) instead of the resolved path's package name. This applies to all NpmPackage return paths inresolve_specifier.Let me implement the fix now.
- The oxc resolver follows pnpm's symlinks/aliases and resolves to
- Assistant
Let me look at the
try_style_condition_package_resolutionfunction too since it also returns NpmPackage. - Assistant
Now I have a complete understanding of the issue. The fix needs to be applied in the
resolve_specifierfunction. When a bare specifier like@std/csv/stringifyresolves through node_modules (even through.pnpm), the resolved path contains the aliased package name (@jsr/std__csv). But the user's intent (declared in package.json and used in the import specifier) is@std/csv.The fix: when resolving a bare specifier that ends up in node_modules, prefer
extract_package_name(specifier)(the declared/import name) overextract_package_name_from_node_modules_path(resolved_path)(the resolved source name).Let me implement the fix now.
- Assistant
Now let me look at the full
resolve_specifierfunction to understand the flow and make the fix. - Assistant
Now I understand the complete picture. The fix should be: when resolving a bare specifier that ends up in node_modules, prefer the specifier's declared package name over the resolved path's package name. This handles pnpm catalog source aliases where the resolved name differs from the declared name.
Let me implement the fix in
resolve_specifier. The key locations are:- Line 1137-1149: bare specifier, non-.pnpm path
- Line 1175-1179: bare specifier, canonicalized path
- Line 1205-1209: bare specifier, error canonicalization
For bare specifiers, I should use
extract_package_name(specifier)instead ofextract_package_name_from_node_modules_path(resolved_path). - Assistant
Now I understand the issue deeply. Let me implement the fix. The key insight is:
When a bare specifier like
@std/csv/stringifyresolves through pnpm's catalog source aliases to a different package name in node_modules (@jsr/std__csv), we should prefer the specifier's declared package name (@std/csv) over the resolved path's package name.Let me create a helper function and modify the resolution logic.
- Assistant
Now I understand the issue clearly. Let me implement the fix. The core problem is that when a bare specifier like
@std/csv/stringifyresolves through pnpm's catalog source alias to@jsr/std__csvin node_modules, the resolver extracts@jsr/std__csvas the package name instead of@std/csv.The fix: when resolving a bare specifier and the resolved path is in node_modules, prefer the specifier's package name over the resolved path's package name. This correctly handles pnpm catalog source aliases.
- Assistant
Now I have a clear understanding. The fix should be in
resolve_specifier: when a bare specifier resolves through node_modules, prefer the specifier's declared package name (extract_package_name(specifier)) over the resolved path's package name (extract_package_name_from_node_modules_path(resolved_path)). This correctly handles pnpm catalog source aliases where the declared name differs from the resolved name.Let me implement the fix:
- Assistant
Now let me verify the changes compile correctly.
- Assistant
Good, it compiles. Now let me also apply the fix to the
try_style_condition_package_resolutionfunction, for consistency. - Assistant
The
try_style_condition_package_resolutionfunction also has the same issue - it usesextract_package_name_from_node_modules_pathto get the package name from the resolved path. Let me apply the same fix for consistency. - Assistant
Let me also check if there's a third location in the style resolution function that needs fixing.
- Assistant
There's a third location in the style resolution function that also needs fixing. Let me fix it too.
- Assistant
Wait, I made a mistake with the third style location fix. The original code only returned NpmPackage when
extract_package_name_from_node_modules_pathreturned Some, otherwise falling back to ExternalFile. My fix would always return NpmPackage, which is wrong. Let me fix this properly. - Assistant
Let me verify the compilation again.
- Assistant
Let me run the existing tests to make sure nothing is broken.
- Assistant
All existing tests pass. Let me also run the core tests and the issue-specific tests.
- Assistant
All the pnpm catalog tests pass. Let me also run the broader test suite.
- Assistant
Let me run the full test suite for the graph and core packages to make sure nothing is broken.
- Assistant
All the graph tests pass. Let me also run the core tests.
- Assistant
All tests pass. Let me also run the integration tests.
- Assistant
All 545 integration tests pass. Now let me create a test that specifically verifies the fix for pnpm catalog source aliases. Let me first look at how the existing resolver tests work.
- Assistant
Let me look at the test for
specifier_npm_scheme_scoped_credits_packageto understand the pattern. - Assistant
Let me now create a test to verify the fix works for the pnpm catalog source alias scenario. Let me first look at how tests with real node_modules are set up.
- Assistant
Let me look at the pnpm store path test to understand how pnpm paths are tested.
- Assistant
Let me now look at how the pnpm store path extraction works for source aliases to understand the issue better.
- Assistant
Let me now run a broader set of tests to make sure the fix doesn't break anything.
- Assistant
Good, all 545 integration tests pass. Let me also check the npm scheme tests.
- Assistant
Good. Now let me also check that the
npm:scheme tests still work correctly. - Assistant
Let me now run the full test suite to make sure everything passes.
- Assistant
All tests pass. Now let me also look at
find_unlisted_dependenciesto see if there's an additional place where the specifier's declared name should be preferred over the resolved name. - Assistant
Let me now look at the
find_unlisted_dependenciesfunction to understand how the specifier's package name is used. - Assistant
Let me now also look at the
npm:scheme handling to make sure the fix doesn't affect it. - Assistant
Let me look at the
try_style_condition_package_resolutionchanges I made more carefully to make sure they're correct. - Assistant
Let me now verify the fix by looking at all the changes I made.
- Assistant
Let me now run the full test suite one more time to make sure everything is solid.
- Assistant
All tests pass. Let me now also look at the
resolve_specifierfunction to verify the first fix location is correct (the non-.pnpm bare specifier path). - Assistant
Let me also check that the
unlisted_dependenciesfunction works correctly by looking at how it checks against the specifier's package name. - Assistant
Let me now also check the
find_unlisted_dependenciesfunction to understand howpackage_usage_nameis used there. - Assistant
Let me look at how
import_spans_by_fileis built infind_unlisted_dependencies. - Assistant
Let me look at how
import_spans_by_fileis built infind_unlisted_dependencies: - Assistant
Let me now look at the
find_unlisted_dependenciesfunction more closely to understand how it builds theimport_spans_by_file: - Assistant
Good. The
import_spans_by_fileis built usingedge.target().package_usage_name(), which returns the package name fromNpmPackage. With my fix, this will now correctly be the specifier's declared package name (e.g.,@std/csv) instead of the resolved source name (e.g.,@jsr/std__csv).Let me now run the full test suite one final time to make sure everything is solid.
- Assistant
All tests pass. Let me also run the CLI tests to make sure nothing is broken.
- Assistant
All tests pass (1770+2250+22+6+6+25+7+3+16+4 = 4307 tests). Let me now verify the full diff of my changes.
- Assistant
Let me view the full diff one more time to make sure everything looks correct.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.