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 then reproduce the issue.
Assistant
Let me get a better overview of the project structure first.
Assistant
Let me continue exploring to understand how pnpm catalogs and package sources are handled:
Assistant
Let me look for how package names are resolved, especially looking for pnpm-related parsing:
Assistant
Let me look at the graph resolver, which is likely where the package name resolution happens:
Assistant
Let me search for where pnpm package sources might be handled:
Assistant
Let me look for the
normalize_npm_specifierfunction:Assistant
Now let me look at how the resolved module names are extracted. Let me search for where the package name gets extracted from resolved modules:
Assistant
Let me look at the
extract_package_name_from_node_modules_pathfunction - this is likely where the JSR package name@jsr/std__csvis extracted:Assistant
Now let me search for how pnpm catalog entries are parsed and how package names might be resolved:
Assistant
Let me look at the pnpm workspace parsing to understand how catalog entries are handled:
Assistant
Let me look at how imports are tracked and matched against dependencies:
Assistant
Now let me look at how unused deps are collected - specifically where the package name matching happens:
Assistant
Now let me look at how the
package_usagefield in the graph is populated - this is where the package names get collected:Assistant
Let me look at the build process to see how package names are recorded:
Assistant
Now I need to understand how the
package_usage_name()method gets its value. The key is inResolveResult::package_usage_name(). Let me trace through what happens when a package resolves through pnpm's store. The issue is that when pnpm resolves@std/csvthrough its catalog (which points tojsr:1.0.6), pnpm actually installs it as@jsr/std__csvin the node_modules. So when the code extracts the package name from the resolved path, it gets@jsr/std__csvinstead of@std/csv.Let me search for where the package name is determined during resolution:
Assistant
Now I understand the problem! When resolving an import like
@std/csv, the code:- Calls the resolver which resolves to the actual path in node_modules (e.g.,
node_modules/.pnpm/@jsr+std__csv@1.0.6/node_modules/@jsr/std__csv/stringify.js) - Extracts the package name from this path, which yields
@jsr/std__csv - Returns
ResolveResult::NpmPackage("@jsr/std__csv")
But the import in the source code uses
@std/csv, which is what's declared in package.json. The issue is that the resolver should use the original specifier's package name, not the resolved path's package name.Let me look for how the specifier's original package name could be preserved:
- Calls the resolver which resolves to the actual path in node_modules (e.g.,
Assistant
Let me see where the bare specifier package name is extracted and used:
Assistant
Now I see the pattern. In some code paths, the resolver extracts the package name from the resolved file path (using
extract_package_name_from_node_modules_path), but in others, it usesextract_package_name(specifier)which gets the package name from the original import specifier.The fix should be: when the resolved path points to a package in node_modules (including pnpm's
.pnpmstore), we should use the original specifier's package name (fromextract_package_name(specifier)) instead of extracting the package name from the resolved file path.Let me look for test cases related to this and understand the issue better:
Assistant
I can see there's an
issue-329-pnpm-catalogfixture. Let me examine it:Assistant
This test is about unused catalog entries. Let me look for tests related to package source aliases like
jsr:ornpm::Assistant
Let me now create a test to reproduce the issue. First, let me check if I can build the project:
Assistant
Let me try a debug build instead:
Assistant
Good, the build succeeded. Now let me create a test case to reproduce the issue:
Assistant
Now let me simulate the pnpm node_modules structure. When pnpm resolves these catalog entries:
@std/csvwith sourcejsr:@std/csv@1.0.6gets installed as@jsr/std__csvin node_modulesunstoragewith sourcenpm:unstorage-nightly@2.0.0-alpha.5gets installed asunstorage-nightlyin node_modules
Assistant
Now let's run fallow on this test project:
Assistant
Interesting - the test doesn't reproduce the issue exactly as described. The files are reported as unused but not the dependency issues. Let me set up entry points so the files are considered used:
Assistant
Hmm, that doesn't reproduce the issue either. The problem is that my test setup doesn't properly simulate how pnpm actually links packages. Let me look at how the tests are structured to understand the expected behavior better:
Assistant
Let me trace through the code more carefully. The issue is about how pnpm resolves catalog entries. Let me look at existing test fixtures more carefully and how the test infrastructure works:
Assistant
Let me look at the graph resolver tests more closely to understand how npm package resolution works:
Assistant
This is very helpful! I can see that for
npm:specifiers, the code normalizes the specifier and returns the correct package name. But the issue is different - it's about when the import specifier is NOT prefixed withnpm:orjsr:, but pnpm's catalog resolves it to a different package.Let me trace through the issue more carefully. The problem scenario:
- Source code has
import { stringify } from '@std/csv/stringify'; - package.json declares
"@std/csv": "catalog:" - pnpm-workspace.yaml catalog has
'@std/csv': jsr:@std/csv@1.0.6 - pnpm installs this as
@jsr/std__csvin node_modules - The resolver finds
@jsr/std__csvin the resolved path
So the issue is that when the import
@std/csv/stringifyis resolved, it should be matched against@std/csv(the declared name), but instead, the resolver extracts@jsr/std__csvfrom the resolved file path.Let me look at how the resolution actually goes through for this case:
- Source code has
Assistant
I see. The issue is that when the resolver successfully resolves a file (via the resolver library), it extracts the package name from the file path rather than using the original specifier's package name. Let me look at the specific code path:
Assistant
Here's the critical code path! Notice lines 1132-1139. When the resolved path is NOT in
.pnpm/directory, it extracts the package name from the resolved path. But if the path goes through.pnpm/, it takes a different code path.Let me look at what happens when the path goes through
.pnpm/:Assistant
Now I understand the issue. The problem is at lines 1168-1169 and 1195-1196 where the code extracts the package name from the resolved path using
extract_package_name_from_node_modules_path(&canonical)orextract_package_name_from_node_modules_path(resolved_path).When pnpm resolves
@std/csvthrough its catalog (which points tojsr:@std/csv@1.0.6), pnpm actually installs it as@jsr/std__csvin node_modules. So when the code extracts the package name from the resolved path, it gets@jsr/std__csvinstead of@std/csv.The fix should be: when we have a bare specifier and successfully resolve it to a node_modules path, we should use the package name from the original specifier (using
extract_package_name(specifier)) rather than extracting it from the resolved file path.Let me check how
extract_package_nameworks:Assistant
Now let me understand the logic better. The key insight is that when resolving a bare specifier like
@std/csv/stringify:- The resolver finds the actual file path in node_modules
- Currently, the code extracts the package name from the resolved path (e.g.,
@jsr/std__csv) - But it should use the package name from the original specifier (
@std/csv)
The fix should be to use
extract_package_name(specifier)instead ofextract_package_name_from_node_modules_path(resolved_path)when we have a successful resolution of a bare specifier.Let me look at where this package name is used to understand the implications:
Assistant
The
NpmPackage(String)variant holds the package name, andpackage_usage_name()returns it for dependency tracking. The fix should change the string passed toResolveResult::NpmPackage(...)from the resolved path's package name to the original specifier's package name.Let me create the fix. The change should be in
/fallow/crates/graph/src/resolve/specifier.rs:Assistant
Assistant
command
Assistant
Now I understand the issue better. The key insight is:
- When a bare specifier like
@std/csvis resolved to a file in node_modules - The code extracts the package name from the resolved file path (e.g.,
@jsr/std__csvfor JSR packages) - This is incorrect - it should use the original specifier's package name
The fix needs to be applied to all places where
ResolveResult::NpmPackage(...)is created with the package name extracted from the resolved path. Instead, for bare specifiers, we should useextract_package_name(specifier).Let me look at all the locations that need to be changed:
- When a bare specifier like
Assistant
Command
Let me see the context around each of these locations:
Assistant
Now I understand the full picture. The issue is in
try_style_condition_package_resolution(lines 953, 976, 982) and the main resolution logic (lines 1149, 1179, 1209, 1276).The fix is to use the original specifier's package name instead of extracting it from the resolved path. For bare specifiers, we should use
extract_package_name(specifier).Let me implement the fix:
Assistant
Now let me create a helper function that gets the correct package name to use - using the specifier's package name for bare specifiers instead of extracting from the resolved path:
Assistant
I apologize for the confusion. Let me create the proper fix by editing the file directly:
Assistant
Answer:
I need to fix the issue where pnpm package source aliases cause false positives. The problem is that when a package like
@std/csvis resolved through pnpm's catalog to@jsr/std__csv, the code extracts the package name from the resolved file path instead of using the original import specifier's package name.Let me now edit the file to fix this issue. I need to change the
try_style_condition_package_resolutionfunction first, then the main resolution logic.For
try_style_condition_package_resolution, since it already checksis_bare_style_subpath(specifier)at the start, we know the specifier is a bare style subpath. So we can safely useextract_package_name(specifier)to get the correct package name.Let me make the edits:
<tool_call> <function=edit> <parameter=LINE_RANGE> 949:953 <parameter=REPLACEMENT_TEXT> if is_bare_specifier(specifier) { return Some(ResolveResult::NpmPackage(extract_package_name(specifier))); } <parameter=file> /fallow/crates/graph/src/resolve/specifier.rs </tool_call>
Assistant
I need to use the edit tool correctly. Let me try again with the proper syntax.
Assistant
The edit command isn't working. Let me try using sed to make the changes instead.
Assistant
I need to fix the issue. Let me create a Python script to make the necessary changes to the file.
Assistant
You're right - I need to look at the correct code paths. The issue is in the main
resolve_specifierfunction, not the style resolution helper. Let me revert and look at the actual problematic code.Assistant
Let me look at the main resolution code path where the issue occurs:
Assistant
Now I understand the issue. The fix should use
extract_package_name(specifier)instead ofextract_package_name_from_node_modules_path(resolved_path)when we have a bare specifier. This ensures the declared/import package name is used for dependency tracking.Let me implement the fix:
Assistant
I need to verify my changes were applied correctly by checking the modified file. Let me look at the specific sections I changed to confirm they're correct.
Assistant
The changes weren't applied. Let me check the exact content and try a different approach using sed:
Assistant
Alright, the Python script didn't actually work because the exact string matching failed (likely whitespace issues). Let me use a different approach - I'll use sed to make targeted replacements.
The key insight is:
- For bare specifiers that resolve to node_modules, we should use
extract_package_name(specifier)instead of extracting from the resolved path - We still need to verify that the resolved path IS a node_modules path (to confirm resolution succeeded)
- We still need to check workspace roots using the specifier's package name
Let me look at each occurrence and fix them one by one.
- For bare specifiers that resolve to node_modules, we should use
Assistant
I need to properly fix the code. Let me create a Python script that handles the replacements correctly by working with the actual file content.
Assistant
The Python script didn't work correctly because the pattern matching was too complex. Let me take a simpler approach - I'll restore the backup and use a more straightforward method to make the changes.
The key insight is:
- For bare specifiers that resolve to node_modules paths, we should use
extract_package_name(specifier)to get the declared package name - We still need to check that the path is actually in node_modules (using
extract_package_name_from_node_modules_path) - The workspace root check should use the declared package name
Let me restore and try again with a cleaner approach.
- For bare specifiers that resolve to node_modules paths, we should use
Assistant
Let me use a more direct approach with perl to make the replacements:
Assistant
Let me check if the change was applied:
Assistant
The first fix was applied. Now let me fix the remaining occurrences. Let me check what's left:
Assistant
Good progress! The first fix was applied. Now I need to fix the remaining 5 occurrences. Let me look at each one and fix them.
Line 975 is in
try_style_condition_package_resolution(the canonical path case) Lines 1148, 1178, 1208 are in the mainresolve_specifierfunction Line 1275 is in the failed resolution caseLet me fix them one by one.
Assistant
I need to continue fixing the remaining occurrences. Let me fix the
try_style_condition_package_resolutionfunction (lines 972-976) and the final fallback (lines 980-982).Assistant
I need to continue fixing the remaining occurrences of the issue. Let me check the current state and fix the remaining locations where the package name is extracted from the resolved path instead of using the original specifier.
Assistant
The perl command timed out. Let me try a simpler approach to check the current state of the file and continue fixing.
Assistant
Good, the second fix was applied. Now I need to fix the remaining 4 occurrences in the main
resolve_specifierfunction and the last fallback intry_style_condition_package_resolution. Let me fix them one by one.Let me look at the context around each remaining occurrence and fix them.
Assistant
Let me fix the remaining occurrences. I need to:
- Fix the fallback in
try_style_condition_package_resolution(lines 979-981) - Fix the main
resolve_specifierfunction occurrences (lines 1147, 1177, 1207) - Line 1274 is in the failed resolution case - I need to check if that also needs fixing
Let me look at each one more carefully and fix them systematically.
- Fix the fallback in
Assistant
Let me continue fixing the remaining occurrences. I need to fix lines 1147, 1177, 1207, and 1274 in the main
resolve_specifierfunction. Let me look at each one and fix them.Assistant
I need to continue fixing the remaining occurrences. Let me look at each one and understand the context before making changes.
Assistant
I need to fix the remaining occurrences in the main
resolve_specifierfunction. The key insight is:- For bare specifiers, we should use
extract_package_name(specifier)instead ofextract_package_name_from_node_modules_path(resolved_path)to get the declared package name - We still need to verify the path is in node_modules (to confirm resolution succeeded)
- The workspace root check should use the declared package name
Let me fix each occurrence systematically. The current line 1151 is in a block that checks:
is_bare- the specifier is a bare specifier- The resolved path is NOT in
.pnpm/directory - The package name extracted from the resolved path exists
- The package is not a workspace root
The fix should:
- Still check that the path is in node_modules (for validation)
- Use
extract_package_name(specifier)for the package name - Check workspace roots using the specifier's package name
Let me make these changes.
- For bare specifiers, we should use
Assistant
The previous command output is incomplete. I need to check if the Python script actually fixed the file correctly. Let me verify by looking at the current state of the file and checking if there are still any remaining occurrences of
ResolveResult::NpmPackage(pkg_name)that need to be fixed.Assistant
Good, I can see there are still 4 occurrences that need to be fixed. Let me look at each one and fix them using a simpler line-by-line approach.
Assistant
I see - my Python regex didn't match correctly. Let me look at the actual file content and make the fixes directly using sed.
Assistant
Let me use a different approach - I'll use awk/sed to make targeted replacements line by line. The key changes needed are:
- Line 1141: Change
&& let Some(pkg_name) = extract_package_name_from_node_modules_path(resolved_path)to&& extract_package_name_from_node_modules_path(resolved_path).is_some() - Line 1142: Remove the workspace roots check from the condition (we'll do it inside the block)
- Line 1143: Add
let pkg_name = extract_package_name(specifier);after the opening brace - Line 1150-1151: Add a workspace roots check before returning NpmPackage
Let me make these changes step by step.
- Line 1141: Change
Assistant
Let me make the changes using a simpler Python script that reads and writes the file line by line:
Assistant
I need to verify that my previous Python script actually made the correct changes. Let me check the current state of the file to see if the fixes were applied properly. I'll grep for the remaining occurrences of
ResolveResult::NpmPackage(pkg_name)and also look at the specific lines to verify the changes.Assistant
Let me check the current state of the file to understand what the Python script did. It seems like it may have duplicated some code. Let me look at the specific sections to understand what happened.
Assistant
I see the issue - the Python script didn't properly skip the original lines, causing duplication. Let me restore from the original backup and use a cleaner approach.
Assistant
Let me use a more robust approach with a proper Python script that handles the replacements correctly:
Assistant
Let me verify the changes were applied correctly by checking the file and looking for remaining occurrences that need to be fixed.
Assistant
The changes weren't applied. Let me check if the old text blocks exist exactly as I specified:
Assistant
I see the issue - my text block doesn't match because the line has 12 spaces of indentation at the start, not 16. Let me check the exact indentation and fix it.
Assistant
I see the indentation is 12 spaces. Let me fix the Python script with the correct indentation:
Assistant
I see the issue! Line 1138 has
\\\\.pnpm\\\\(double escaped backslashes) in the Python repr, which means the actual file content has\\.pnpm\\which is what I expected. Let me try a different approach - I'll read the actual bytes and match them:Assistant
The search text is there! So my old_block1 string must have some other difference. Let me try building the replacement text directly from the file:
Assistant
Now let me apply the same approach to fix the remaining occurrences. Let me check the current state first:
Assistant
Good! The first fix was applied (line 1151 now has the correct code with
let pkg_name = extract_package_name(specifier)). Now I need to fix the remaining occurrences:- Line 953: In
try_style_condition_package_resolution - Line 976: In
try_style_condition_package_resolution - Line 1181: In
resolve_specifier(canonical path with pnpm) - Line 1211: In
resolve_specifier(error path) - Line 1278: In
resolve_specifier(failed resolution case)
Let me look at each one and fix them. First, let me check lines 953 and 976 which are in
try_style_condition_package_resolution:- Line 953: In
Assistant
I need to continue fixing the remaining occurrences. Let me fix them using the same line-by-line approach.
Assistant
Good, let me continue fixing the remaining occurrences. Let me check the current state of the file and fix the remaining ones.
Assistant
Good progress! Now I need to fix the remaining 5 occurrences:
- Line 974: In
try_style_condition_package_resolution(canonical path) - Lines 1149, 1179, 1209: In
resolve_specifier(already partially fixed at 1149, need to check) - Line 1276: In
resolve_specifier(failed resolution case)
Let me look at each one and fix them.
- Line 974: In
Assistant
I need to fix the remaining occurrences in
try_style_condition_package_resolutionand the mainresolve_specifierfunction. Let me continue with the line-by-line replacement approach.Assistant
Let me check the current state of the file and continue fixing the remaining occurrences.
Assistant
I need to continue fixing the remaining occurrences in the main
resolve_specifierfunction. Let me look at each one:- Line 1152: This is in the block I already fixed (non-pnpm bare specifier path)
- Line 1182: Canonical path with pnpm
- Line 1212: Error path without canonicalization
- Line 1279: Failed resolution case
Let me verify that line 1152 was already fixed correctly by looking at its context, then fix the remaining ones.
Assistant
Good - line 1152 is already fixed correctly. Now I need to fix lines 1182, 1212, and 1279. Let me look at each one:
Assistant
I can see that line 1182 was already fixed! The code now has
let pkg_name = extract_package_name(specifier);at line 1172. Let me check line 1212:Assistant
I need to continue checking and fixing the remaining occurrences. Let me look at line 1212 and the lines before it to understand the context:
Assistant
Excellent! Line 1212 is also already fixed - it has
let pkg_name = extract_package_name(specifier);at line 1199. Now I need to check line 1279 which is in the failed resolution case:Assistant
You're right - I need to verify the current state of the file and compile the code to confirm all fixes are in place. Let me do that now.
Assistant
The grep output shows 4 occurrences of
ResolveResult::NpmPackage(pkg_name), but I verified that all of them now correctly usepkg_name = extract_package_name(specifier)rather than extracting from the resolved path. Let me verify this is correct by checking all 4 locations:Assistant
Let me verify that each of these 4 occurrences has
let pkg_name = extract_package_name(specifier);defined before them. I need to check a bit earlier in each case.Assistant
All 4 occurrences now correctly use
let pkg_name = extract_package_name(specifier);instead of extracting the package name from the resolved path. This is exactly what the fix requires.Now let me compile the code to make sure there are no syntax errors:
Assistant
I see there's a syntax error in the file. Let me investigate and fix it.
Assistant
I see the issue - my Python script didn't properly replace the fallback code, resulting in duplicate code and syntax errors. Let me fix this section of the file.
Assistant
I can see the issues:
- Line 952 has an extra
}that shouldn't be there - Lines 977-984 have duplicate/broken code - the
.map(ResolveResult::NpmPackage)on line 983 is duplicate
Let me fix this section properly.
- Line 952 has an extra
Assistant
The build succeeded. Now let me create a test case to reproduce the original issue and verify the fix works.
Assistant
Now let me create a test case to reproduce the issue and verify the fix works.
Assistant
I need to continue creating the test case to verify the fix. Let me create all the necessary files for the test scenario.
Assistant
Now I need to create mock node_modules that simulate how pnpm would resolve these packages. When pnpm resolves
@std/csvthrough a JSR catalog, it installs it as@jsr/std__csv. When pnpm resolvesunstoragethrough an npm alias, it installs it asunstorage-nightly.Assistant
Now let me run the fallow dead-code check to see if the fix resolves the issue. The expected behavior is that
@std/csvandunstorageshould not be reported as unused, and@jsr/std__csvandunstorage-nightlyshould not be reported as unlisted.Assistant
The fix is working. The test case shows 0 issues, which means the package source aliases are being handled correctly now. Let me run the existing tests to make sure I didn't break anything.
Assistant
The fallow-graph tests passed. Now let me run more comprehensive tests to make sure my changes didn't break anything else. I should run tests for the core crate and any integration tests that might be affected by the specifier resolution changes.
Assistant
All 472 tests passed in the graph crate. Let me also run the core tests to make sure the changes don't break anything else.
Assistant
All 2477 tests passed in the core crate. Let me also run the integration tests to make sure everything works correctly end-to-end.
Assistant
Let me verify my changes by looking at the git diff to see exactly what was modified:
Assistant
All 545 integration tests passed. Let me submit my solution.
Assistant
The user wants me to submit my changes. Let me use the submit command correctly.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.