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 working with. Let's first check what kind of project this is.
- Assistant
Let me explore the repository structure more.
- Assistant
This looks like a Rust project (Cargo.toml, crates directory). Let me explore more to understand the structure.
- Assistant
This is a Rust project called "fallow" which is a linter/static analysis tool for TypeScript/JavaScript. The issue is about false positives when pnpm package sources are used through catalogs. Let me explore the crates directory to understand the project structure better.
- Assistant
Let me understand the project structure better and look for the dead-code analysis code.
- Assistant
Let me explore the crates directory to understand the structure better.
- Assistant
Let me examine the unused_deps.rs file to understand how it works.
- Assistant
Let me look at the pnpm.rs plugin file to understand how pnpm catalogs are handled.
- Assistant
Now let me look at the pnpm_catalog.rs file to understand how catalogs are handled.
- Assistant
Let me search for how pnpm package source aliases are handled in the codebase.
- Assistant
Let me look at the resolve module to understand how dependencies are resolved.
- Assistant
Let me look at the resolve module.
- Assistant
Let me look at the resolve/mod.rs file to understand how dependencies are resolved.
- Assistant
Let me look at how the resolver resolves imports and what it returns. Let me check the ResolvedModule type to see what information is stored about resolved imports.
- Assistant
Let me look at how dependencies are resolved from import paths. Let me search for how unused_deps.rs matches imports to declared dependencies.
- Assistant
Let me look at the find_unlisted_dependencies function to understand how it works.
- Assistant
Let me look at how
package_usage_name()is determined in the resolver. - Assistant
Let me check how the resolver determines the package name. Let me look at the fallbacks.rs file.
- Assistant
Let me search for where the resolver handles pnpm virtual store paths.
- Assistant
Let me look at how
extract_package_name_from_node_modules_pathworks. - Assistant
Now let me understand the issue better. The problem is about pnpm catalog aliases. When you have a pnpm-workspace.yaml with:
catalog: '@std/csv': jsr:1.0.6 unstorage: npm:unstorage-nightly@2.0.0-alpha.5And then in package.json you have:
{ "dependencies": { "@std/csv": "catalog:", "unstorage": "catalog:" } }And in your source code you import:
import { stringify } from '@std/csv/stringify'; import { createStorage } from 'unstorage';The issue is that pnpm resolves these differently:
@std/csv→ resolves to@jsr/std__csv(jsr: protocol)unstorage→ resolves tounstorage-nightly(npm: protocol with different name)
The tool is reporting:
@std/csvas unused/unlisted (shouldn't be)@jsr/std__csvas unlisted (shouldn't be)unstorageas unused/unlisted (shouldn't be)unstorage-nightlyas unlisted (shouldn't be)
- Assistant
Now let me look at the specifier resolution code to understand how
ResolveResult::NpmPackageis created. - Assistant
I need to understand the full flow better. Let me search for where
extract_package_nameis called and where package names are being resolved. - Assistant
Let me look at how
extract_package_nameis implemented to understand what it does with the import specifier. - Assistant
Let me look at how the resolver handles
oxc_resolverresults. - Assistant
Let me search for how the resolver resolves and what happens when it resolves to a node_modules path.
- Assistant
Let me look more carefully at the resolution flow. The issue is that when pnpm resolves
@std/csvthroughjsr:1.0.6, the actual path in node_modules becomes something like@jsr/std__csv, andextract_package_name_from_node_modules_pathpicks up the wrong name. Let me look at the key resolution function more carefully. - Assistant
Let me look at the resolve_specifier function.
- Assistant
Let me understand the full resolution flow. Let me look at lines around where
NpmPackageis created to understand the issue better. - Assistant
Let me also look at how pnpm catalog entries are loaded and stored.
- Assistant
Let me look at how the workspace configuration stores pnpm catalog data.
- Assistant
Let me look at the WorkspaceInfo structure to understand what's stored.
- Assistant
Let me look at the ResolvedConfig structure to see if it stores catalog data.
- Assistant
Let me search for where pnpm catalog data is loaded.
- Assistant
Let me look at the existing integration test for pnpm catalog to understand how it works.
- Assistant
Let me look at the existing test fixtures to understand how pnpm catalog works.
- Assistant
Let me now look at the path_info module to understand how package names are extracted.
- Assistant
Now I understand the issue clearly. When resolving
@std/csv/stringify, the resolver extracts the package name from the resolved node_modules path (@jsr/std__csv) instead of using the original specifier's package name (@std/csv). Let me look at how the existing test for Supabase handles this, as it deals withnpm:specifiers. - Assistant
Now I understand the structure better. The key issue is that when pnpm uses catalog entries like:
catalog: '@std/csv': jsr:1.0.6 unstorage: npm:unstorage-nightly@2.0.0-alpha.5And packages reference these with
catalog:protocol:{ "dependencies": { "@std/csv": "catalog:", "unstorage": "catalog:" } }pnpm resolves
@std/csvto@jsr/std__csvandunstoragetounstorage-nightlyin node_modules.The resolver in fallow is extracting the package name from the node_modules path, so it gets and instead of the original declared names and .
- Assistant
Let me look more carefully at the exact resolution path. I need to understand how
@std/csv/stringifygets resolved and where the package name is extracted. - Assistant
Now I understand the full picture. The issue is in the resolution flow. When pnpm resolves
@std/csvthrough a catalog entry likejsr:1.0.6, the actual package in node_modules becomes@jsr/std__csv. The resolver extracts the package name from the node_modules path, getting@jsr/std__csv, instead of using the declared package name@std/csv.The fix should be in
crates/graph/src/resolve/specifier.rswhereNpmPackageresults are created. When the specifier is a bare specifier and the resolved path's package name differs fromextract_package_name(specifier), we should use the specifier's package name since that's what's declared in the user'spackage.json.Let me first create a test fixture to reproduce the issue, then implement the fix.
- Assistant
Now I understand the full picture. The issue is in the resolution flow. When pnpm resolves
@std/csvthrough a catalog entry likejsr:1.0.6, the actual package in node_modules becomes@jsr/std__csv. The resolver extracts the package name from the node_modules path, getting@jsr/std__csv, instead of using the declared package name@std/csv.The fix should be: when we resolve a bare specifier and get a
NpmPackage(resolved_name)from node_modules, butextract_package_name(specifier)gives a different name, we should use the specifier's package name instead, because that's the name the user declared inpackage.json.Let me first look at the existing tests to understand the test patterns:
- Assistant
Now let me look at the relevant resolution code more carefully. I need to understand all the places where
NpmPackageis returned from node_modules path extraction. Let me search for all such places: - Assistant
Let me look at lines around each of these to understand the context:
- Assistant
Let me see what function this is and whether the specifier is available at this point:
- Assistant
Let me look at the specific areas where
NpmPackageis returned and understand how to fix them: - Assistant
Now I understand the issue. The resolver extracts package names from the resolved node_modules path, but when pnpm uses catalog source aliases (like
jsr:ornpm:different-name), the resolved path contains a different name than what the user declared/imported.The fix should be: when a bare specifier resolves to a node_modules path, prefer the specifier's package name (from
extract_package_name(specifier)) over the resolved path's package name, since the specifier is what the user declared and imported.Let me create a helper function to apply this logic consistently. First, let me see what the current code does in the function context:
- Assistant
Now let me create a comprehensive understanding. The issue is that when pnpm resolves packages through catalog entries with
jsr:ornpm:aliases, the resolver extracts the wrong package name from the resolved node_modules path. Let me now implement the fix. The approach should be: when the specifier is a bare specifier, preferextract_package_name(specifier)over the resolved path's package name. - Assistant
Now let me implement the fix. The approach is: when we extract a package name from a resolved node_modules path, if the specifier is a bare specifier and
extract_package_name(specifier)gives a valid package name, prefer it over the resolved path's package name. This handles pnpm catalog aliases where the resolved path uses a different name than the declared/import name. - Assistant
Let me understand the flow more carefully. I need to look at where
NpmPackageis created from resolved paths: - Assistant
Let me create a helper function to prefer the specifier's package name over the resolved path's package name when the specifier is a bare specifier:
- Assistant
Let me look at how the issue manifests more closely. When pnpm resolves
@std/csv/stringify, the node_modules path will be something likenode_modules/.pnpm/@jsr+std__csv@1.0.6/node_modules/@jsr/std__csv/stringify.js. The resolver extracts@jsr/std__csvfrom the path. But the user imported@std/csv/stringifyand declared@std/csvas a dependency.The fix should be: when the specifier is a bare specifier, prefer
extract_package_name(specifier)over the resolved path's package name. Let me create a helper function to do this: - Assistant
Let me look at the
try_style_condition_package_resolutionfunction more carefully: - Assistant
Now I understand the problem completely. Let me trace through the flow:
- User imports
@std/csv/stringify(bare specifier) - The resolver resolves it to a path like
node_modules/.pnpm/@jsr+std__csv@1.0.6/node_modules/@jsr/std__csv/stringify.js extract_package_name_from_node_modules_pathextracts@jsr/std__csvfrom the path- This becomes
NpmPackage("@jsr/std__csv") - But the declared dependency is
@std/csv, so it's reported as unlisted
The fix should be: when the specifier is a bare specifier and the resolved package name from node_modules differs from
extract_package_name(specifier), use the specifier's package name instead. This handles pnpm catalog aliases where the resolved name differs from the declared name.Let me implement the fix. I need to create a helper function that applies this logic consistently:
- User imports
- Assistant
Now let me implement the fix. First, I'll add a helper function to
path_info.rs: - Assistant
Now let me add the helper function and apply the fix. I'll add it to
path_info.rsfirst: - Assistant
Now let me understand the complete flow. I need to modify the resolver to prefer the specifier's package name over the resolved path's package name when the specifier is a bare specifier. Let me look at all the places where
NpmPackageis returned fromextract_package_name_from_node_modules_path: - Assistant
Let me implement the fix. I need to:
- Add a helper function to prefer the specifier's package name over the resolved path's package name
- Apply this helper in all places where
NpmPackageis returned fromextract_package_name_from_node_modules_path
- Assistant
I see the function was inserted incorrectly - it duplicated the doc comment. Let me fix this:
- Assistant
Let me fix the file properly. The new function was inserted in the wrong location:
- Assistant
I see there are issues - a stray
}on line 82 and duplicate doc comments. Let me fix this: - Assistant
I still see duplicate doc comments. Let me fix that:
- Assistant
The file is messy. Let me rewrite the affected section properly:
- Assistant
I see there's still a duplicate line. Let me fix that:
- Assistant
Good, the file looks clean now. Let me verify the function was added correctly:
- Assistant
Now I need to apply the
preferred_package_namefunction in the resolver. Let me update the specifier.rs file to use this function in all the places whereNpmPackageis returned fromextract_package_name_from_node_modules_path. Let me first look at the import section of specifier.rs: - Assistant
Now I need to import
preferred_package_namein specifier.rs and apply it in all the places whereNpmPackageis created fromextract_package_name_from_node_modules_path. Let me update the import first: - Assistant
I see the import was duplicated. Let me fix that:
- Assistant
Good, the imports are correct now. Let me now apply the
preferred_package_namefunction in all the places whereNpmPackageis created fromextract_package_name_from_node_modules_path. Let me look at the first location intry_style_condition_package_resolution: - Assistant
Let me search for the exact lines where
NpmPackageis created fromextract_package_name_from_node_modules_path: - Assistant
Let me look at the context around each
NpmPackage(pkg_name)to understand which ones need the fix: - Assistant
Now I need to apply the fix. The approach is: when the specifier is a bare specifier and the resolved package name from node_modules differs from
extract_package_name(specifier), prefer the specifier's package name. Let me update the first location intry_style_condition_package_resolution: - Assistant
Let me look at all the locations more carefully:
- Assistant
This line 1276 is in the
ResolveFileAttempt::Failedbranch. Let me look at wherepkg_nameis defined here. - Assistant
Good. The last
NpmPackage(pkg_name)at line 1276 already usesextract_package_name(specifier)directly (which is correct - this is the failure path). The other 5 locations need the fix. Let me update them one by one.Let me start with line 953 in
try_style_condition_package_resolution: - Assistant
Now let me apply the fix. I need to change
NpmPackage(pkg_name)toNpmPackage(preferred_package_name(specifier, &pkg_name))at each location. Let me start with line 953: - Assistant
Good. Now let me update the remaining locations. Line 976 in
try_style_condition_package_resolution: - Assistant
Now let me update line 982 (the map call). This is a bit different since it uses
.map(ResolveResult::NpmPackage): - Assistant
Good. Now let me update the remaining locations in
resolve_specifier. Let me find line 1149: - Assistant
Now let me update line 1149:
- Assistant
Now let me update line 1149.
- Assistant
Good. Now let me update line 1179:
- Assistant
Good. Now let me update line 1209 (in the error branch):
- Assistant
Now let me update line 953 in
try_style_condition_package_resolution: - Assistant
Good, line 953 is already updated. Let me verify line 976 in the same function:
- Assistant
Now let me verify all the changes are correct by searching for remaining unmodified
NpmPackage(pkg_name)patterns: - Assistant
Good, only the correct one remains (line 1276, which uses
extract_package_name(specifier)directly). Now let me also check the fallbacks.rs file for any similar patterns: - Assistant
I see there are also places in
fallbacks.rswhereNpmPackageis created fromextract_package_name_from_node_modules_path. Let me look at them: - Assistant
Let me look at the context of this function to understand if the specifier is available:
- Assistant
Let me look at the other location in fallbacks.rs at line 365:
- Assistant
Let me look at the full function to understand the context:
- Assistant
The fallbacks.rs locations are:
try_path_alias_fallback(line 69): This is for path aliases like@/components, which are not bare specifiers. Thepreferred_package_namefunction would return the resolved name unchanged sinceis_path_aliasreturns true. So no change needed here.
The
try_path_alias_fallbackfunction is for path aliases like@/componentswhich are not bare specifiers, sopreferred_package_namewould return the resolved name unchanged. No change needed there.The SCSS fallback is also unlikely to be affected by pnpm catalog aliases since CSS imports don't typically use the same package name patterns.
Now let me verify the changes compile correctly:
Now let me verify that the changes compile correctly by running the tests:
- Assistant
It's downloading dependencies. Let me wait for it to complete.
- Assistant
Now let me run the tests to make sure the changes don't break anything. Let me run the resolve module tests:
- Assistant
Now let me run the path_info tests to make sure the new function is also tested:
- Assistant
Now let me run the full test suite to make sure everything passes:
- Assistant
Let me also run the core integration tests to make sure the pnpm catalog tests pass:
- Assistant
Let me also run the Supabase edge function test to make sure
npm:specifiers still work correctly: - Assistant
Good, all tests pass. Now let me add a test for the new
preferred_package_namefunction: - Assistant
Let me add tests for the new
preferred_package_namefunction: - Assistant
I see there's a duplicate test. Let me fix that:
- Assistant
I see there's a duplicate
mod proptestsdeclaration. Let me fix that: - Assistant
Now let me run the tests to make sure the new tests pass:
- Assistant
I see there's a duplicate
extract_package_name_bare_scope_onlytest. Let me remove the duplicate: - Assistant
I see there's a duplicate
mod proptests. Let me fix this: - Assistant
There's still a duplicate
mod proptests. Let me fix this properly: - Assistant
There are still extra closing braces. Let me look at the file and fix the issue:
- Assistant
Let me fix the file properly by removing the extra closing braces:
- Assistant
There's still a duplicate
mod proptests. Let me fix it: - Assistant
Good, the file looks correct now. Let me run the tests to verify:
- Assistant
All tests pass including the new ones. Now let me run the full test suite to make sure everything is working correctly:
- Assistant
Let me also run the core integration tests to make sure everything still works:
- Assistant
Let me also run the unused_deps tests to make sure nothing is broken:
- Assistant
Let me run the full integration test suite to make sure nothing is broken:
- Assistant
All 545 integration tests pass. Let me now verify the existing pnpm catalog integration tests work correctly:
- Assistant
Now let me create a test fixture to verify the fix works for the specific case described in the issue. Let me look at the existing test fixtures to understand the pattern:
- Assistant
Let me run a comprehensive test to make sure the changes are correct and complete:
- Assistant
All 478 tests pass for the fallow-graph package. Now let me verify the fix works correctly by running a broader test suite. Let me also run the full test suite to make sure nothing is broken:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.