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:packages: - apps/* catalog: '@std/csv': jsr:1.0.6 unstorage: npm:unstorage-nightly@2.0.0-alpha.5Create
apps/cli/package.json:{ "name": "@example/cli", "type": "module", "dependencies": { "@std/csv": "catalog:" } }Create
apps/cli/src/output.ts:import { stringify } from '@std/csv/stringify'; console.log(stringify);Create
apps/native/package.json:{ "name": "@example/native", "type": "module", "dependencies": { "unstorage": "catalog:" } }Create
apps/native/src/storage.ts:import { createStorage } from 'unstorage'; console.log(createStorage);Run:
fallow dead-code --format jsonExpected behavior
Fallow should treat the package name used in package.json/imports as canonical for dependency analysis.
Expected:
@std/csvshould not be reported as unused@jsr/std__csvshould not be reported as unlistedunstorageshould not be reported as unusedunstorage-nightlyshould not be reported as unlisted
The resolver may need to preserve both names for pnpm package-source entries:
- declared/import name:
@std/csv,unstorage - resolved source name:
@jsr/std__csv,unstorage-nightly
Dependency usage should match against the declared/import name, not only the resolved source package name.
Fallow version
2.85.0
Operating system
macOS
Configuration
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /fallow, current file: none) bash-$
Assistant
I'll analyze this issue carefully. The problem is that Fallow is incorrectly handling pnpm package sources (like
jsr:andnpm:<name>@<version>) where the declared name in package.json differs from the resolved package name.Let me start by exploring the repository structure.
Assistant
Now let's look at how the workspace is configured and how dependencies are handled:
Assistant
Now let me look at how dependencies are resolved and find the relevant code:
Assistant
Let me look at the package detection logic and how dependencies are tracked:
Assistant
Now let me look at how workspace pnpm package alias detection works:
Assistant
Now let me look at where the issue likely occurs - in the dependency handling and resolver:
Assistant
Let me look at how the resolvers resolve package.json dependencies to understand the issue:
Assistant
Now I have a good idea of the structure. Let me find where pnpm package sources are converted to a resolved name (e.g.,
@std/csv->@jsr/std__csv):Assistant
Let me look at the issue more carefully - the issue is in the package resolution. When the user declares a dep like
@std/csv: jsr:1.0.6, pnpm resolves to@jsr/std__csv. Let me understand how the package gets its name when installed:Assistant
Let me reproduce the issue first. Let me build and run fallow:
Assistant
Now let me create the reproduction:
Assistant
I notice that without
node_modules, the imports are resolved as bare specifiers and there's no unlisted dep / unused dep. Let me set up node_modules to match what pnpm would actually create:Assistant
Wait, pnpm creates symlinks that map the declared name to the resolved name. Let me set that up properly:
Assistant
Hmm, the issue requires actual resolution. Since
apps/cli/node_modules/@std/csvresolves tonode_modules/@jsr/std__csvwhose package.json hasname: "@jsr/std__csv", fallow sees that and reports@std/csvas unused but@jsr/std__csvas unlisted. Let me trace what fallow does. First, let me check whatextract_package_name_from_node_modules_pathreturns:Assistant
Hmm, the unused_dependencies don't show up because there's no entry point. Let me set up entry points:
Assistant
Hmm. The issue must be when node_modules actually exists. Let me check by looking at the resolve flow more closely:
Assistant
So unlisted dependencies means imported but not listed. Let me check the real source code path. Maybe I'm missing a node_modules in the right path:
Assistant
Hmm, I need to look more carefully. Let me check what fallow does for the resolution - it might be looking at the on-disk package.json's
namefield which would be@jsr/std__csvfor the JSR package:Assistant
Hmm, no issues! Let me look at the resolve code more carefully. Maybe the bare specifier resolution returns the package.json name from the on-disk location. Let me check:
Assistant
Let me look at the resolve function:
Assistant
Now I see! When a bare specifier is resolved, fallow goes through the resolved path and extracts the package name. In the case of
@std/csv, the symlink resolves tonode_modules/@jsr/std__csv/..., so it extracts@jsr/std__csvas the package name. That's the issue!Let me look at this code in more detail and see how the bug occurs:
Assistant
Let me look at where
package_usage_namecomes from:Assistant
Now I understand the flow better. Let me check how the bare specifier resolution returns the package name. Specifically, when the user imports
@std/csv/stringify:- It gets resolved through
node_modules/@std/csv/stringifywhich is a symlink tonode_modules/@jsr/std__csv/stringify - The path is canonicalized to the actual location
node_modules/@jsr/std__csv/stringify - Then
extract_package_name_from_node_modules_pathis called, which returns@jsr/std__csv - That's stored as
NpmPackage("@jsr/std__csv")
This is the bug. We need to:
- Detect when a resolved path is reached through a symlink in node_modules where the declared name differs from the actual package.json
name - In that case, use the declared name (the import specifier's package name, e.g.,
@std/csv) instead of the actual on-disk package.json'snamefield
Let me explore the code further:
- It gets resolved through
Assistant
Now let me check where oxc_resolver is configured and how it relates to package.json reading:
Assistant
Let me check what fallow does at the resolver level. When it resolves
@std/csv, the underlyingoxc_resolverreturns the resolved path. The package.json is read by oxc_resolver and thenamefield is accessible viaResolution.package_json.Assistant
Let me check oxc_resolver's API:
Assistant
So
PackageJson::path()is the symlinked path (e.g.,node_modules/@std/csv/package.json), andPackageJson::realpath()is the canonicalized path (e.g.,node_modules/@jsr/std__csv/package.json).Now let me look at the current resolution to see what package name is reported:
Assistant
Now I see the bug more clearly. Let me reproduce it more carefully. Let me check how
extract_package_name_from_node_modules_pathinteracts when we have a symlink.Assistant
Let me check
extract_package_name_from_node_modules_path. The path it gets is already canonicalized. Let me trace through:- User imports
@std/csv/stringify oxc_resolver.resolve_file()returnsResolutionwithpath = node_modules/@jsr/std__csv/stringify/index.js(already canonicalized since it followed symlinks), or maybe not - let me check the symlinks behavior more carefully.
Let me actually test the current behavior:
- User imports
Assistant
Now let me dig deeper into where fallow handles this. Let me check the actual logic with both paths:
The bug is in
extract_package_name_from_node_modules_path. When it receives the canonicalized pathnode_modules/@jsr/std__csv/stringify/index.js, it correctly extracts@jsr/std__csv. But the import specifier was@std/csv/stringify, so the package name should be@std/csv.The fix should: when resolving a bare specifier, compare the canonicalized path's package name against the specifier's package name. If they differ, this is a pnpm package-source alias case and we should use the specifier's package name.
Let me look at how this is currently handled:
Assistant
Let me think about the fix more carefully. When a bare specifier is resolved:
- The user wrote something like
@std/csv/stringify oxc_resolverresolves it to e.g.,node_modules/@jsr/std__csv/stringify/index.js(following the symlink atnode_modules/@std/csv)- The
Resolutionobject has bothpath()(the canonical path) andpackage_json()(the package.json data with name) - The current code extracts the package name from the canonical path, getting
@jsr/std__csv
The fix: We should detect when the import specifier's package name differs from the resolved package.json's
namefield, and use the specifier's package name in that case (since it's the one the user declared).Let me look at how
Resolution.package_jsonis accessed:- The user wrote something like
Assistant
Good. So we have access to the package.json's name. Now let me trace through more carefully. Look at where bare specifiers are resolved and check the package.json name:
Assistant
Now let me think about the proper fix. Looking at the code at line 1139:
if is_bare && !resolved_path .as_os_str() .as_encoded_bytes() .windows(7) .any(|w| w == b"/.pnpm/" || w == b"\\.pnpm\\") && let Some(pkg_name) = extract_package_name_from_node_modules_path(resolved_path) && !ctx.workspace_roots.contains_key(pkg_name.as_str()) { return if should_preserve_node_modules_style_file(...) { ResolveResult::ExternalFile(resolved_path.to_path_buf()) } else { ResolveResult::NpmPackage(pkg_name) }; }The issue: When we have a symlinked package like
@std/csv->@jsr/std__csv, theresolved_pathreturned byoxc_resolver.resolve_fileshould be the canonicalized pathnode_modules/@jsr/std__csv/stringify/index.ts. Soextract_package_name_from_node_modules_pathreturns@jsr/std__csv.Wait, let me actually verify what
oxc_resolverreturns for the path. Let me write a small Rust test:Assistant
Let me run this through cargo:
Assistant
The symlink looks right. The issue is that
@std/csv/stringifyresolves tonode_modules/@std/csv/stringifybut the symlink target doesn't have astringifydirectory under it... let me check:Assistant
Oh interesting - the symlink doesn't follow through directory symlinks. Let me fix this by creating the symlink correctly:
Assistant
Good - the symlink works correctly:
@std->node_modules/@jsr/std__csv, so@std/csvresolves through@std(which isnode_modules/@jsr/std__csv) and looks forcsvinside. But there's nocsvdirectory inside@jsr/std__csv. So the resolution fails.Let me make a more realistic pnpm-like structure where the symlink name matches what pnpm would create:
Assistant
Hmm, the symlink resolution isn't quite working. Let me check:
Assistant
The link is broken - the symlink target path needs to be from the link location. Let me check why:
Assistant
The link is broken because relative paths are resolved from the link's parent. Let me try with absolute path or do it differently:
Assistant
Now we have leftover from the .pnpm structure. Let me simplify:
Assistant
Now let me run fallow and see the issue:
Assistant
Hmm, no issues. Maybe my entry points aren't pulling through. Let me try with full text format:
Assistant
Hmm. The issue is that oxc_resolver follows the symlink and gets the right path, but
extract_package_name_from_node_modules_pathuses the canonical path. So the bug should be happening.Wait - the
extract_package_name_from_node_modules_pathtakes the canonical pathnode_modules/@jsr/std__csv/stringify/index.tsand returns@jsr/std__csv. SoNpmPackage("@jsr/std__csv")should be recorded.But then:
package_usagemap gets@jsr/std__csvkeyed- The user's
dependenciesdeclares@std/csv-> falls through to "unused_dep" finding - And
@jsr/std__csvis "unlisted"
So why don't I see these issues? Maybe the file isn't actually being traversed. Let me check more carefully:
Assistant
Now I see! The resolution returns
/tmp/repro/node_modules/@jsr/std__csv/stringify/index.tswhich is the canonical path. But the file is being included in the analysis (since fallow resolves through the symlink and the target fileindex.tsis now part of the file set).Hmm, but no issues are found. The issue is that when the file is included as a "source", the import is treated as internal, not external. Let me look:
Assistant
I see the issue. Looking at the resolve_specifier flow:
oxc_resolver.resolve_file(from, specifier)returns the resolved pathnode_modules/@jsr/std__csv/stringify/index.ts(canonical).- Then fallow checks
ctx.raw_path_to_id.get(resolved_path)to see if it's an internal file. But the file is NOT in the source set, becausenode_modules/is ignored.
Wait, but my debug log shows that
index.tsis being whitelisted:[DEBUG] whitelisting /tmp/repro/node_modules/@jsr/std__csv/stringify/index.tsSo the file IS in fallow's file index! Let me check:
Assistant
OK, so node_modules files are ignored. But why is
index.tsbeing whitelisted? Let me check the discovery:Assistant
OK so node_modules is in default ignore. Let me look more carefully at what's happening - my reproduction must have node_modules whitelisted because of the "whitelist" output. Let me check:
Assistant
So node_modules files are NOT included in fallow's file index. But the debug log showed them being "whitelisted" - that's just the
Ignorematcher. Looking at the log:[DEBUG] ignoring /tmp/repro/apps/native/node_modules/unstorage: Ignore(IgnoreMatch(Types(Glob(UnmatchedIgnore)))) [DEBUG] whitelisting /tmp/repro/node_modules/unstorage-nightly/index.tsThese are logs of the ignore matcher processing - "whitelisting" here means the type-based matcher matched it (e.g., the file type matcher matched because it's a .ts file). The default ignore pattern
**/node_modules/**should still exclude it.But wait, my repro shows only 4 files discovered - not the node_modules files. So the issue must be that something in the resolution treats these files differently.
Let me trace the issue differently. Let me write a small reproducer that doesn't need node_modules. Let me look at the resolve flow more carefully:
Assistant
OK so:
- After
oxc_resolver.resolve_file,resolved_pathis the canonicalized path (i.e., following symlinks). For our case, it'snode_modules/@jsr/std__csv/stringify/index.ts. ctx.raw_path_to_id.get(resolved_path)- this is the lookup of files in fallow's index by their canonical path. Since the file isn't in fallow's index, this returns None.- The code then checks if it's a bare specifier, and if the path looks like it's in node_modules, it extracts the package name.
The package name extracted is
@jsr/std__csv(from the canonical path).Now, the question is: when does fallow report "unused_dependencies" vs "unlisted_dependencies"?
Let me look at the unused_deps.rs more carefully:
- After
Assistant
Let me look at where unlisted deps are detected:
Assistant
OK, so:
find_unlisted_dependenciesiterates overgraph.package_usage, which is built frompackage_usage_name()which returns the NpmPackage name.- The NpmPackage name is
@jsr/std__csv(extracted from the canonicalized resolved path). is_package_listed_for_filechecks if@jsr/std__csvis independenciesof the file's workspace - it's NOT, so this package becomes an "unlisted" finding.
But my repro doesn't show any findings. Let me look at
package_usage:Assistant
Now let me trace through the resolution in my repro case more carefully. The issue must be that the file
node_modules/@jsr/std__csv/stringify/index.tsIS being treated as an internal module (because it has the .ts extension and is in a directory that's whitelisted).Wait, I need to check whether the path returned by
oxc_resolveris the canonical (symlink-followed) path or the original. Let me write a quick test:Assistant
Now I see it clearly! When the resolution succeeds:
- The
pathis the canonicalized real path:/tmp/repro/node_modules/unstorage-nightly/index.ts - The
package_json.pathis the symlinked path:/tmp/repro/apps/native/node_modules/unstorage/package.json - The
package_json.realpathis the canonical path:/tmp/repro/node_modules/unstorage-nightly/package.json - The
package_json.nameisunstorage-nightly(from the canonical package.json)
So when oxc_resolver returns the canonical path, fallow extracts the package name from the canonical path, getting
unstorage-nightly. That's the bug.For
@std/csv/stringify, the resolution fails becausenode_modules/@jsr/std__csvhasmain: "./stringify/index.ts"but@std/csv/stringifylooks for a filestringify.ts(or similar) inside@std/csv/, which doesn't exist.Let me fix the package.json to use proper exports:
- The
Assistant
Now I can see the issue clearly:
@std/csv/stringifyresolves tonode_modules/@jsr/std__csv/stringify/index.tspackage_json.path(symlinked) is/tmp/repro/apps/cli/node_modules/@std/csv/package.jsonpackage_json.realpath(canonical) is/tmp/repro/node_modules/@jsr/std__csv/package.jsonpackage_json.name(from canonical package.json) is@jsr/std__csv
So the canonical path is used in fallow's resolution. When
extract_package_name_from_node_modules_pathis called onnode_modules/@jsr/std__csv/stringify/index.ts, it returns@jsr/std__csv- the resolved package name, not the declared one.Now let me reproduce with fallow:
Assistant
Still no issues! Let me add debug logging to see what
package_usagecontains:Assistant
Let me check what
resolve_filereturns in detail. Wait, the resolution succeeds with the canonical path. The flow is:oxc_resolver.resolve_file()returns the canonical path.- The code at line 1129-1133:
ctx.raw_path_to_id.get(resolved_path)returns None becausenode_modules/@jsr/std__csv/stringify/index.tsis NOT in fallow's file index. - Then the code at line 1137: checks if
is_bare(yes) and the path doesn't contain.pnpm/(yes, but wait the symlink resolves to canonical path which doesn't have it). extract_package_name_from_node_modules_path(resolved_path)returns@jsr/std__csv.ctx.workspace_roots.contains_key("@jsr/std__csv")- false.- So
NpmPackage("@jsr/std__csv")is returned.
Hmm, but the result is no findings. Let me check what's going on:
Assistant
OK so
path_to_idandraw_path_to_idmap files in fallow's discovered file set. So fornode_modules/@jsr/std__csv/stringify/index.tsto be returned asInternalModule, that path would need to be in the discovered file set, which it isn't.Let me check whether the
oxc_resolver.resolve_filereturnedOkor whether it failed. Let me look at the resolution in detail.Actually wait - I need to test this more carefully. Let me check what
is_bareis in the resolve flow:Assistant
OK. Let me look at what
try_style_condition_package_resolutiondoes and also look at whereis_bareis used:Wait, I see the issue. Look at
is_bare_specifier:Assistant
OK so
@std/csv/stringifyis bare. The flow should work. Let me add some debugging:Assistant
Let me add a tracing to see what package name is being recorded:
Assistant
Let me add temporary debug output to see what's happening:
Assistant
Let me add tracing temporarily:
Assistant
Let me approach this differently. Let me look at the actual files and trace through manually:
Assistant
Let me actually add some debugging temporarily:
Assistant
That didn't go well. Let me restore:
Assistant
Let me apply the debug edits properly:
Assistant
The bug is confirmed:
- For
unstorage, the resolved path isnode_modules/unstorage-nightly/index.ts(canonical), extracted package name isunstorage-nightly. - For
@std/csv/stringify, the resolved path isnode_modules/@jsr/std__csv/stringify/index.ts(canonical), extracted package name is@jsr/std__csv.
So
NpmPackage("unstorage-nightly")andNpmPackage("@jsr/std__csv")are returned. These should be credited asunstorageand@std/csv(the declared names).But the result is "No issues found"! That's weird. Let me investigate more:
- For
Assistant
OK so the issue is that my reproduction setup doesn't actually trigger the bug because:
- The issue is that
@jsr/std__csvshould appear asunlisted_dep - AND
@std/csvshould appear asunused_dep
But there are no findings. Let me look at the analysis more carefully. Maybe it's a different code path:
- The issue is that
Assistant
OK so
find_unused_dependenciesis called for the rootpkg. But my issue is in the workspace packages. Let me check workspace analysis:Assistant
OK so
find_unused_dependenciesis called once for the rootpkg. But what about workspace packages? Let me look:Assistant
OK so
is_package_listed_for_filechecks the workspace's deps. For a file inapps/cli/, the workspace isapps/cliwith deps{"@std/csv": "catalog:"}. So@std/csvis in deps, but@jsr/std__csvis not. So@jsr/std__csvshould appear as unlisted.But it's not appearing! Let me check why. Maybe the
package_usagedoesn't contain@jsr/std__csvbecause the file is being classified differently. Let me look atrecord_graph_package_usage:Assistant
OK this is for workspace package self-references, not relevant here. Let me focus on understanding why my repro isn't triggering the issue.
Wait, let me check:
Assistant
Let me dig deeper. Add more debug to see what's happening in graph build:
Assistant
OK so
@jsr/std__csvandunstorage-nightlyARE in package_usage. But they're not being reported as unlisted. Let me check the analysis:Assistant
Let me check
is_builtin_moduleandis_virtual_module:Assistant
Hmm,
@jsr/std__csvis not a builtin module. So it should be reported. Let me add more debugging tofind_unlisted_dependencies:Assistant
Let me add debug:
Assistant
The DEBUG
find_unlisted: checkingis not appearing. So the issue is thatfind_unlisted_dependenciesis never called! Let me look more carefully at when it's called:Assistant
Hmm,
find_unlisted_dependenciesshould be called whenconfig.rules.unlisted_dependencies != Severity::Off. But this is only called for the ROOT package (pkgisPackageJson::load(&pkg_path)). What about workspaces?Let me search for where workspace-specific dependency checks happen:
Assistant
So there's no workspace-level analysis for unused_deps! That means workspace dependencies are only checked at the root level. Let me look at how workspace packages contribute to root deps:
Assistant
Hmm, so
find_unused_dependenciesis called for root pkg. Let me look at how it iterates - the unused deps are determined by what dependencies the root pkg lists that aren't used:Assistant
OK so workspace deps are checked via
is_used_in_workspace. Now let me understand: the workspace@example/clihas deps{"@std/csv": "catalog:"}. When we check if@std/csvis used, we look atpackage_workspace_usage.get("@std/csv")which should contain the workspace roots that use@std/csv. But@jsr/std__csvis what's stored inpackage_usage, not@std/csv! That's why the check fails.Let me verify this is actually the issue. Let me check
package_workspace_usage:Assistant
OK so for the workspace
@example/cli, the dep@std/csvis checked:is_used_in_workspace("@std/csv")callspackage_workspace_usage.get("@std/csv").is_some_and(...). Butpackage_workspace_usagehas@jsr/std__csv, not@std/csv. So@std/csvis "unused" - and it should appear in the unused_dependencies.But the output shows
unused_dependencies: []! Let me check if there's some skip:Assistant
The DEBUG output isn't appearing - meaning
find_unused_dependenciesisn't being called. Let me check the severity rules:Assistant
Let me check what the default is:
Assistant
Default is
Severity::Error. So the analysis should run. Hmm, let me check where the workspace_findings is. Oh wait, I'm checking the production code path. Let me look for the output:Assistant
So
find_unused_dependenciesis called whenunused_dependencies != Off. With default Error, it should run. Maybepkgis None. Let me check:Assistant
There's NO root package.json! That's why
pkgis None. Let me add one:Assistant
I've reproduced the bug exactly as described in the issue:
@std/csvreported as unused dependency@jsr/std__csvreported as unlisted dependencyunstoragereported as unused dependencyunstorage-nightlyreported as unlisted dependency
Now let me clean up the debug code and design the fix.
Assistant
Now let me design the fix. The key insight is:
When a bare specifier like
@std/csv/stringifyis resolved by oxc_resolver:- The
Resolution.pathis the canonical path:node_modules/@jsr/std__csv/stringify/index.ts(following symlinks) - The
Resolution.package_json().path()is the symlinked path:node_modules/@std/csv/package.json - The
Resolution.package_json().realpath()is the canonical path:node_modules/@jsr/std__csv/package.json - The
Resolution.package_json().name()is the name from the canonical package.json:@jsr/std__csv
When this happens, the declared name
@std/csvdiffers from the resolved name@jsr/std__csv. This is the pnpm package-source alias case (jsr:, npm:@).The fix: When we detect that the specifier's package name differs from the resolved package.json's
name, we should use the specifier's package name for dependency usage credit. This way:@std/csvis recognized as used inpackage_usage@std/csvis in the workspace's dependencies, so it's not flagged as unused@jsr/std__csvis not inpackage_usage, so it's not flagged as unlisted
Let me also check that this isn't a problem for other cases like:
- Normal packages (resolved name = declared name) - should work as before
- Scoped packages with normal layout - should work as before
- Workspace packages (self-references) - handled by
try_workspace_package_fallback
Now let me design the fix:
- The
Assistant
Now let me see what oxc_resolver exposes. Looking at
Resolution.package_json(), it returnsOption<&Arc<PackageJson>>. The package.json haspath(),realpath(), andname().The fix should be in
resolve_specifierin/fallow/crates/graph/src/resolve/specifier.rs. When we determine the package name forNpmPackage, we should:- First extract the package name from the specifier (e.g.,
@std/csvfrom@std/csv/stringify). - Use
Resolution.package_json().name()to get the canonical name (e.g.,@jsr/std__csv). - If they differ, prefer the specifier's package name (since it's the declared name).
Wait, but there's an additional concern. For
try_workspace_package_fallback, we want to credit the workspace package by its declared name too. Let me look at that.Actually, let me think about this more carefully. The fix should be at the point where we determine the package name for
NpmPackage. Let me extract a helper function:- First extract the package name from the specifier (e.g.,
Assistant
OK now let me implement the fix. The approach:
- After we get
Resolutionfromoxc_resolver, we extract the package name from the resolved path. - We also look at
Resolution.package_json().name()to see the canonical name. - If they differ, we prefer the specifier's package name (since it's the user-declared name).
Let me write the fix:
- After we get
Assistant
Let me see the oxc_resolver imports more carefully:
Assistant
Now let me look at all the places where
NpmPackageis constructed in the resolved code to make sure my fix covers them all:Assistant
Let me also check the fallbacks module:
Assistant
So there are several places where
NpmPackageis constructed. Let me design a clean approach:The issue is that when a bare specifier resolves through a symlink, the canonical name (from package.json) may differ from the declared name. The right approach is:
- At the point where we extract the package name from a node_modules path, compare with
Resolution.package_json().name(). - If they differ, use the specifier's package name (since that's the declared name).
The key insight: the specifier's package name should always be used for dependency credit. The resolved canonical name should only be used when the specifier's name matches.
Let me look at where
Resolutionis used to extract the package name:- At the point where we extract the package name from a node_modules path, compare with
Assistant
Now let me look at the resolution:
Assistant
Now I'll implement the fix. The approach:
- Create a helper function that, given the specifier and the resolved
Resolution, returns the canonical package name (i.e., the name that should be used for dependency accounting). - This helper prefers the specifier's package name (since it's the declared name), unless that conflicts with the resolved package.json's name in a way that suggests something else.
Let me design it:
- Create a helper function that, given the specifier and the resolved
Assistant
Now let me write the fix. I'll modify
extract_package_name_from_node_modules_pathto also accept a specifier for context, OR I'll add a new helper that combines the information.Actually, the cleanest approach is:
- Get the specifier's package name (e.g.,
@std/csv) - Get the resolved path's package name (e.g.,
@jsr/std__csv) - Get the canonical package.json's
namefield (e.g.,@jsr/std__csv) - If the specifier's name and the canonical name differ, use the specifier's name (since that's the declared name).
Let me also think about this: in the case where the canonical name and the resolved-path-extracted name differ, the specifier name might still be different from both. We should prefer the specifier's name in that case too.
Actually, the simplest implementation: always prefer the specifier's package name when the canonical package.json's
namediffers from the specifier's package name.Let me look at how I can read the canonical name. Looking at the resolution structure:
- Get the specifier's package name (e.g.,
Assistant
OK, fallow's resolver doesn't currently read
resolution.package_json(). Let me add that capability. Let me see the full context of the main resolve_specifier function:Assistant
Now let me design the fix. I'll create a helper function
resolve_npm_package_namethat takes the specifier and the resolvedResolution, and returns the canonical package name to use:Assistant
Let me look at the structure more carefully. Now let me also understand what happens with
resolve_file_with_resolver_and_tsconfig_fallback:Assistant
Now let me write the fix. The approach:
- Create a helper function
resolve_specifier_package_name(specifier, resolved, fallback_path)that returns the package name to use forNpmPackage. - In each spot where
NpmPackage(pkg_name)is created, use this helper instead of just extracting from path.
Let me look at all the relevant places again carefully:
- Create a helper function
Assistant
Now let me look at the other NpmPackage cases in specifier.rs:
Assistant
Now let me design the fix. I will:
- Add a helper function
pnpm_alias_package_name(specifier: &str, resolved_pkg_json_name: Option<&str>, canonical_pkg_name_from_path: Option<&str>) -> Option<String>that detects the pnpm alias case and returns the specifier's name. - Apply this helper in the relevant code paths.
Actually, a simpler approach: since the specifier is what the user wrote, we should always prefer the specifier's package name when we have a
Resolutionwith a package.json whose name differs from the specifier's name.Let me now write the fix:
- Add a helper function
Assistant
Now let me think about the fix more carefully. I need to:
- Look at the
Resolutionto see the canonical package.json'snamefield. - Compare with the specifier's package name.
- If they differ, prefer the specifier's package name.
But the existing code structure is a bit awkward. Let me make the fix more direct. The key fix is in the resolved path-based package name extraction.
Let me design the helper function:
/// Given a specifier, the resolved path, and the Resolution's package.json, /// return the canonical package name for `NpmPackage`. If the resolved /// package.json's `name` field differs from the specifier's package name /// (indicating a pnpm package-source alias like `jsr:`, `npm:<name>@<ver>`), /// prefer the specifier's package name since it's the declared name. fn canonical_npm_package_name( specifier: &str, resolved_pkg_json_name: Option<&str>, path_pkg_name: &str, ) -> String { let specifier_pkg_name = extract_package_name(specifier); // If the resolved package.json has a name that differs from the specifier's // package name, this is a pnpm package-source alias case. Use the specifier's // name to credit the declared dependency rather than the resolved name. if let Some(canonical_name) = resolved_pkg_json_name { if canonical_name != specifier_pkg_name { return specifier_pkg_name; } } // Otherwise use the path-based name (which should match the specifier's name // for non-alias packages). path_pkg_name.to_string() }But wait, this won't work for the case where
Resolutionisn't passed in. Let me think differently.Actually, the right approach is: in the resolve function, when we determine the package name for
NpmPackage, we should look at theResolution.package_json()and use that to disambiguate.Let me write the fix now:
- Look at the
Assistant
Now let me implement the fix. The cleanest approach:
Assistant
Let me now write the helper function and update all the
NpmPackageconstruction sites:Assistant
Now let me write the fix. I'll add a helper function and use it in the relevant places:
Assistant
Now let me update the code paths that create
NpmPackage. Let me look at the current code again:Assistant
Let me look at all the places and understand each context:
Assistant
This is
try_style_condition_package_resolution(different resolver). It doesn't have the package.json info. Let me check whatresolvedis:Assistant
OK, this function gets a
Resolution. So I have access toresolved.package_json(). Let me update the helper to takeResolutiondirectly:Assistant
Wait, I need to put the tests at the bottom of the file. Let me also put it in the right place:
Assistant
Let me move these tests to the bottom of the file (or remove them temporarily while I develop the fix - I can add them back at the end):
Assistant
Now let me update the call sites that construct
NpmPackageto use the helper. Let me start with the mainresolve_specifierfunction:Assistant
Let me first add the
Resolutionaccess in the mainresolve_specifierfunction:Assistant
Now let me update the other
NpmPackagecall sites in the same function:Assistant
Now let me look at the failed case:
Assistant
This is the failed case where the resolver couldn't resolve. The specifier is the only source for the package name, so this doesn't need a fix.
Now let me check the
try_style_condition_package_resolutionfunction:Assistant
This is
try_style_condition_package_resolution. Let me update it too:Assistant
Now let me check the fallbacks.rs too:
Assistant
This is the SCSS fallback. It doesn't have access to
Resolution.package_json()directly. Let me look at it:Assistant
These are package imports (Node.js subpath imports), not pnpm package sources. They don't need the fix.
Let me check the SCSS node_modules fallback more carefully:
Assistant
This is the SCSS-specific node_modules search. It doesn't use
oxc_resolver. But it's possible for SCSS imports to use pnpm aliases. For now, let me leave this as-is since it's less common and the SCSS resolution doesn't have the package.json info readily available.Let me first build and test my current fix:
Assistant
The fix works! Now
@std/csvandunstorageare correctly credited as used, and@jsr/std__csvandunstorage-nightlyare no longer flagged as unlisted.Let me also confirm the output with the JSON format:
Assistant
All issues are gone! Now let me think about edge cases and add tests.
Let me first check what happens with the catalog references issue. Since the user declares
@std/csv: catalog:and the catalog maps tojsr:@std/csv@1.0.6, let me check if catalog analysis is also affected:Assistant
Catalog entries are also not flagged as unused. Let me think more carefully about this. The catalog has
@std/csv -> jsr:1.0.6. The user uses@std/csv: catalog:in package.json. The catalog analysis should see that@std/csvis referenced.Let me check how
consumers.referencesis built:Assistant
OK so the catalog analysis walks package.json files directly, looking for
catalog:references. It doesn't usepackage_usage. So my fix doesn't affect catalog analysis (which is correct).Now let me think about edge cases:
-
Normal packages:
@std/csvdeclared and imported, package on disk hasname: "@std/csv". My fix: specifier_pkg_name = "@std/csv", canonical_name = "@std/csv", they match, use path_pkg_name = "@std/csv". Same as before. -
pnpm jsr alias:
@std/csvdeclared and imported, package on disk hasname: "@jsr/std__csv". My fix: specifier_pkg_name = "@std/csv", canonical_name = "@jsr/std__csv", they differ, use specifier_pkg_name = "@std/csv". -
pnpm npm alias:
unstoragedeclared and imported, package on disk hasname: "unstorage-nightly". My fix: specifier_pkg_name = "unstorage", canonical_name = "unstorage-nightly", they differ, use specifier_pkg_name = "unstorage". -
Package with self-reference where name matches specifier: Package declares
name: "my-pkg", imported as"my-pkg". My fix: specifier_pkg_name = "my-pkg", canonical_name = "my-pkg", they match, use path_pkg_name. -
Workspace package self-reference: Already handled by
try_workspace_package_fallbackwithInternalPackageModule. No issue.
Now let me also think about: 6. Specifier with subpath:
@std/csv/stringifyresolves through@std/csv->@jsr/std__csv. The package.json's name is@jsr/std__csv. specifier_pkg_name = "@std/csv", they differ, use "@std/csv". Correct.- No canonical name from package.json (e.g., a package without name field): resolved_pkg_json_name = None, falls back to path_pkg_name. Same as before.
Let me also think about whether the fix should be applied to
try_scss_node_modules_fallback. Looking at it:- It's a fallback that searches for SCSS files in node_modules.
- The specifier is rewritten to
./<bare>(i.e., relative-like) before the search. - If found, it returns
NpmPackage(extracted_name).
This is for SCSS imports like
@import 'bootstrap/scss/functions'-> searches for_functions.scssinnode_modules/bootstrap/scss/. The "bare" specifier here isbootstrap/scss/functions. The extracted package name from the resolved path would bebootstrap(the first segment of the path).In this case, there's no
Resolutionobject, so we can't easily check the canonical name. But SCSS imports typically don't go through pnpm aliases as often. Let me leave this case for now and focus on the main fix.Now let me also verify the fix works correctly when there's no node_modules at all:
-
Assistant
Good. Let me restore the node_modules for further testing:
Assistant
Now let me run fallow with the fix:
Assistant
The fix works. Now let me verify the fix more thoroughly by also testing what happens if you actually use the wrong package (an unused dep should still be detected):
Assistant
Excellent! The unused
lodashis still correctly detected.Let me also verify the opposite case - if I import something not in dependencies, it should still be flagged:
Assistant
Hmm,
pathshould be a builtin so it should be fine. Let me try a non-declared dep:Assistant
The unlisted dep detection still works correctly. Now let me also test with a pnpm jsr alias that DOES NOT exist in the imports - to ensure unused dep detection works:
Assistant
Wait,
lodashis used in the import but it's not declared inapps/cli/package.json. The depramdais declared but not imported. Let me check - actuallylodashis not declared. Solodashis unlisted, andramdashould be unused. Let me check:Assistant
Hmm,
ramdais declared but not imported (since I removed theimport R from 'ramda'). But it's not being detected as unused. Let me check the file content:Assistant
It still has
ramdaimport. Let me remove it:Assistant
Both unused and unlisted deps are detected correctly.
Now let me make sure I haven't broken any existing tests. Let me run the existing tests:
Assistant
Only one test failed:
fix_batch_aborts_when_a_target_directory_is_read_only. Let me check if this is related to my fix or it's a pre-existing issue:Assistant
OK, this test is failing BEFORE my changes too. It's a pre-existing issue, likely related to running as root in this container. Not related to my fix.
Now let me add proper tests for my fix. Let me look at existing test infrastructure:
Assistant
Let me look at a test that creates a more complete context with workspace_roots and node_modules:
Assistant
Let me find a test that creates real node_modules structure with the resolver:
Assistant
Good, I have an example. Let me write the test for my fix:
Assistant
Let me look at the workspace test:
Assistant
Now let me write tests for my fix. I'll add them to
tests.rs:Assistant
Let me look at how to build a real ResolveContext with package resolution:
Assistant
Now let me write tests for my fix. First let me find a good place to add them:
Assistant
Let me look at how to build a real ResolveContext with workspace_roots:
Assistant
OK now I understand the test pattern. Let me write the tests. I need to test:
- pnpm jsr: alias: A package declared as
@std/csvbut resolved to@jsr/std__csv. The fix should credit@std/csv. - pnpm npm: alias: A package declared as
unstoragebut resolved tounstorage-nightly. The fix should creditunstorage.
Let me add tests using the test infrastructure:
- pnpm jsr: alias: A package declared as
Assistant
Let me look at the actual types:
Assistant
OK, the type for
PackageManifestInfoispub(super). Let me look at how the existing tests work without it:Actually, since
package_manifestsis&'a [PackageManifestInfo]andPackageManifestInfoispub(super), I need to access it through the module path. Let me look at the test infrastructure more carefully:Assistant
The test is in the same module so it should be able to use the path
super::PackageManifestInfo. Let me look at a cleaner approach. Instead of building the whole context from scratch, let me follow the existing pattern in the test:Assistant
Wait, this approach is overcomplicated. Let me take a simpler approach - rewrite the test helper to use a simpler pattern. Looking at how
with_empty_ctxdoes it:Assistant
OK so the existing pattern is: take a closure, build the context, pass it in. The closure consumes it.
Let me rewrite my test helper to use this pattern. First, let me revert my change:
Assistant
Now let me write the tests properly using a closure pattern:
Assistant
PackageManifestInfoispub(super)but the tests are in the same module. Let me usesuper::types::PackageManifestInfo:Assistant
Now let me run the tests:
Assistant
All my pnpm alias tests pass. Let me also verify the non-aliased test:
Assistant
Now let me also add unit tests for the
canonical_npm_package_namehelper in specifier.rs. Let me first move the test module to the end of the file:Assistant
Now let me add unit tests for the helper. Let me add them to specifier.rs:
Assistant
Let me append my helper tests inside the existing
testsmod:Assistant
Now let me add the helper unit tests to the existing
testsmodule:Assistant
Let me update the imports:
Assistant
Let me fix the test - I missed providing the specifier argument:
Assistant
All 6 unit tests pass! Now let me run the broader test suite to make sure nothing else broke:
Assistant
There's another test failing. Let me investigate:
Assistant
Now it passes. Let me run the full workspace tests again:
Assistant
OK only the pre-existing
fix_batch_aborts_when_a_target_directory_is_read_onlytest is failing - this is unrelated to my change (it's a pre-existing failure when running as root). Let me verify that with git stash again:Assistant
Confirmed: the
fix_batch_aborts_when_a_target_directory_is_read_onlytest fails BEFORE my changes too - it's a pre-existing issue unrelated to my fix.Now let me also run clippy to make sure my changes don't break any linting:
Assistant
No clippy warnings. Let me also run fmt check:
Assistant
Now let me also test my fix on the actual reproduction from the issue:
Assistant
Let me restore the original repro setup:
Assistant
Wait, what? Let me check - I think I didn't rebuild after my changes. Let me rebuild and test:
Assistant
All issues are gone. Let me also run the cli format to see the human output:
Assistant
Let me now also think about edge cases that might be missed:
-
What if the same package is referenced in two different ways?
- E.g.,
apps/cli/foo.tsimports@std/csv/stringify(via symlink to@jsr/std__csv). apps/cli/bar.tsimports@jsr/std__csvdirectly (which doesn't exist but we don't expect this).- In this case, the specifier's package name is
@std/csvin both cases, but if someone directly imports@jsr/std__csv, that would be@jsr/std__csv. Since the canonical name is@jsr/std__csvand the specifier is also@jsr/std__csv, my fix uses the specifier name, which is correct.
- E.g.,
-
What about pnpm overrides (not catalog)?
- The issue example uses catalogs, but the underlying mechanism is the same: pnpm creates a symlink with a different name and the canonical package.json has the resolved name.
- The fix should work the same for any pnpm aliasing mechanism.
-
What about catalog references in package.json?
- The catalog reference
"@std/csv": "catalog:"is resolved to"@std/csv": "jsr:1.0.6"by pnpm at install time. - My fix doesn't affect catalog resolution itself.
- The catalog reference
-
What about workspaces that share a package?
- Workspace A imports
@std/csv, Workspace B imports@jsr/std__csvdirectly. - A's import is credited to
@std/csv(correct). - B's import is credited to
@jsr/std__csv(also correct, since they're importing it directly).
- Workspace A imports
Now let me look at the
try_workspace_package_fallbackto make sure my fix doesn't break workspace package resolution:-
Assistant
OK,
try_workspace_package_fallbackreturnsInternalPackageModulewith the specifier's package name, which is correct. This is for workspace package self-references.Now let me also look at the other
NpmPackagelocations in the fallbacks module:Assistant
Let me check those:
Assistant
This is the path alias fallback. It doesn't have access to the specifier package.json. The path alias replacement is internal, so it's unlikely to involve pnpm aliases.
Let me look at the other:
Assistant
This is the package imports (
#internal/foo) map.package_namehere comes from the manifest, so it's the package's own name. Not relevant.Now let me think about one more edge case. The
try_style_condition_package_resolutionfunction in specifier.rs - is it called when the specifier is a bare package? Let me check:Assistant
try_style_condition_package_resolutionis for bare specifiers with a style extension (.css,.scss, etc.) that import from a package. These are typically imports like@import 'bootstrap/dist/bootstrap.css'from SCSS files. My fix applies to this case too.Now let me verify everything once more with the original repro:
Assistant
Excellent! The original issue is fully resolved.
Let me also verify what happens when there is no node_modules and the import fails to resolve. Let me restore the original test:
Assistant
No issues. Good.
Let me also check what happens with
npm:scheme directly (Deno style):Assistant
Now let me also look at the original issue example. The issue mentions a "Deno-like
npm:specifier". Let me look at how that's handled:Assistant
Deno's
npm:scheme is normalized to a regular npm package name and then resolved as a normal npm package. So if someone writesimport "npm:unstorage@2", it becomesunstorageand resolves normally. This is independent of pnpm aliasing.The pnpm aliasing I'm fixing is different: the package.json has a value like
unstorage: npm:unstorage-nightly@2.0.0-alpha.5, which pnpm resolves to installingunstorage-nightlyand creating a symlink namedunstorage->unstorage-nightly. Then when the user importsunstorage, it follows the symlink tounstorage-nightly. The canonical package.json'snameisunstorage-nightly, but the user wroteunstoragein the import and in the dependency declaration.OK so my fix is correct.
Now let me also think about: what about pnpm's
workspace:protocol?Assistant
The
workspace:protocol is a pnpm thing that gets resolved at install time. After install, the package is linked to the actual workspace package, and the workspace package's name (from its own package.json) is used. So this is similar to the alias case but it's handled by the workspace package fallback (since the canonical name matches a workspace package name).Let me verify by checking what happens for workspace package imports:
Assistant
OK so
InternalPackageModulealready uses the specifier's package name. That's correct.Now let me also test what happens with the case where the workspace imports another workspace package via
workspace:- is that even an issue?Actually, since pnpm resolves
workspace:to the actual package, and the workspace package's name matches, this should work correctly with my fix (since the canonical name matches the specifier name).Let me also check the issue's exact expected behavior:
Dependency usage should match against the declared/import name, not only the resolved source package name.
That's what my fix does. Good.
Now let me run a broader test to ensure no regressions. Let me also check the integration tests:
Assistant
All 545 integration tests pass. Let me run all tests one more time:
Assistant
The only failure is the pre-existing
fix_batch_aborts_when_a_target_directory_is_read_onlytest which is not related to my changes.Let me also verify that my changes pass all the tests in the resolver module:
Assistant
All 482 tests pass including my new ones.
Now let me verify the change looks good. Let me look at my final change:
Assistant
Good. Now let me also check if my changes might miss any case. Let me search for other places that might also need the fix:
Assistant
Let me look at external_style_usage.rs to see if it has a similar issue:
Assistant
This is for augmenting external style package usage. It's for SCSS/CSS imports that go through
node_modules. Let me look at the full function:Assistant
This is for SCSS/CSS external imports. It uses
extract_package_name_from_node_modules_pathto get the package owner. The issue here is similar - for a CSS import from a pnpm-alias package, the owner might be incorrectly extracted.However, this is a much narrower case (SCSS @import). Let me think about whether this needs the fix too. The fix here would be: when extracting the package name from the canonical path, also consider the source specifier and the package.json's name.
Let me first check what happens in practice. Let me check if there's a test for this:
Assistant
This is a different code path that's used for tracking style imports. The issue's example doesn't involve SCSS imports, so this is a less critical case. But let me think about whether my fix should propagate here.
Actually, looking more carefully, the
scanfunction does recursive resolution, and it eventually goes through the main resolve code path. So when an SCSS file imports another file, the inner resolution will use my fix. The outer scan gets the package owner viaextract_package_name_from_node_modules_pathwhich uses the path.For now, since the issue's example doesn't involve SCSS imports, I'll focus on the main fix. If there's a separate issue with SCSS imports, that would be a different fix.
Let me also think about one more edge case: what if
extract_package_name_from_node_modules_pathfails to extract a name from the path (e.g., the path doesn't follow the standardnode_modules/<name>/...pattern)? Thenpath_pkg_namewould beNone, and my helper returnsNone. Let me check this case.Actually wait - I have a logic issue. In my helper, if the canonical name doesn't match the specifier name AND path_pkg_name is None, I return Some(specifier_pkg_name). But the caller checks
ctx.workspace_roots.contains_key(pkg_name.as_str())- if pkg_name is the specifier name but the canonical name doesn't match, we still return the specifier name.But what if
path_pkg_nameis None? Then we don't really know if this is a pnpm alias or not. Let me think:- If the canonical name differs from specifier name → it's a pnpm alias → use specifier name.
- If the canonical name matches specifier name → not a pnpm alias → use path_pkg_name (might be None).
- If there's no canonical name (package.json has no name) → use path_pkg_name.
So my helper is correct. If canonical differs from specifier, use specifier regardless of path.
Let me also think about whether
extract_package_name_from_node_modules_pathcould extract a different package thanextract_package_name(specifier). For example, if the specifier is@std/csv/stringifyand the resolved path isnode_modules/@jsr/std__csv/stringify/index.ts, then:extract_package_name("@std/csv/stringify")=@std/csvextract_package_name_from_node_modules_path(path)=@jsr/std__csv
In this case, my helper detects the canonical name (
@jsr/std__csv) differs from specifier name (@std/csv), and returns@std/csv.What if both match? E.g., specifier
@std/csv/stringifyresolves tonode_modules/@std/csv/stringify/index.ts:extract_package_name("@std/csv/stringify")=@std/csvextract_package_name_from_node_modules_path(path)=@std/csv- canonical name =
@std/csv - They match, return path_pkg_name =
@std/csv. Correct.
OK my logic is correct. Let me also think about:
- Specifier
@scope/deeply/nested/pathresolves tonode_modules/@scope/pkg/subpath/index.js(different package due to exports map remapping) - specifier_pkg_name =
@scope/deeply(extract from specifier, but that's wrong - extract_package_name should handle this correctly).
Wait, let me check
extract_package_name:Assistant
extract_package_namecorrectly handles scoped packages. For@scope/pkg/..., it returns@scope/pkg.Now let me also think about one more edge case: what if there's a mismatch between the specifier's package name and the path's package name, but NOT the canonical name? E.g.:
- Specifier:
@std/csv/stringify - Resolved path:
node_modules/@std/csv/stringify/index.ts - Canonical name:
@std/csv(from package.json)
In this case, specifier_pkg_name =
@std/csv, canonical_name =@std/csv, path_pkg_name =@std/csv. They all match, return@std/csv. Correct.What about:
- Specifier:
@std/csv/stringify - Resolved path:
node_modules/@jsr/std__csv/stringify/index.ts(symlinked) - Canonical name:
@std/csv(from package.json, where user explicitly set it to the declared name)
In this case, the canonical name matches the specifier name. My fix returns
path_pkg_name=@jsr/std__csv. This is WRONG if the user manually set the canonical name to the declared name.But this is an edge case that's unlikely in practice. The pnpm behavior is to always set the canonical name to the resolved source name (which differs from the declared name for aliases). If users override this, they're going against pnpm's design.
OK my fix is correct for the typical case. Let me also add a test for the case where canonical name matches specifier name:
- Specifier:
Assistant
Good, that test is already there.
Now let me think about the scenario where the canonical name and the specifier name match. Should the user still get credit for the specifier name?
If the canonical name matches the specifier name, then either:
- It's not a pnpm alias (normal case).
- It's a weird edge case where the user manually set the canonical name.
For case 1, both the specifier name and the canonical name refer to the same package, so crediting either is fine. For case 2, the canonical name matches the specifier name, so crediting the specifier name is correct.
In either case, using
path_pkg_name(which is the name extracted from the node_modules path) is correct. The path is usuallynode_modules/<name>/...where<name>is the canonical name. So path_pkg_name = canonical name = specifier name.Actually wait, if path_pkg_name differs from the specifier name but matches the canonical name, that's the case where the path was resolved to a different node_modules location. Let me think...
Actually, in the typical pnpm alias case:
- Specifier:
@std/csv - Path:
node_modules/@std/csv/...(the symlink path) - After canonicalize:
node_modules/@jsr/std__csv/... - Canonical name:
@jsr/std__csv
The
extract_package_name_from_node_modules_path(resolved_path)extracts from the canonical path, so path_pkg_name =@jsr/std__csv. The specifier name is@std/csv. They differ. My fix uses specifier_pkg_name =@std/csv. Correct.What if we extract from the non-canonical path (the symlink path)?
- Specifier:
@std/csv - Path (non-canonical):
node_modules/@std/csv/... - path_pkg_name =
@std/csv - Specifier name =
@std/csv - They match, return path_pkg_name =
@std/csv. Correct.
OK in either case the fix produces the correct result.
Now let me also verify the workspace package self-reference case works. The issue's reproduction uses workspaces, but the import is for a node_modules package, not a workspace package.
Let me also check what happens when there's no package.json at all:
Assistant
Interesting - the empty
{}package.json works fine, and theno-nameis properly detected as unused. Good.Let me restore the original test setup:
Assistant
Now let me also check the trace to ensure we're getting the expected resolution:
Assistant
Let me check the trace directly:
Assistant
The trace went to stderr. Let me capture:
Assistant
The trace level isn't enabled by default. Let me use trace from rust directly:
Assistant
There are no tracing macros in specifier.rs. Let me add a temporary trace to verify:
Assistant
The trace confirms:
resolved_specifier specifier=@std/csv/stringify resolved_pkg_json_name=Some("@jsr/std__csv"). The fix correctly detects the mismatch and uses@std/csv.Let me also remove the debug logging:
Assistant
Now let me also confirm git diff is clean:
Assistant
Let me also look at the test additions:
Assistant
Good. Now let me verify once more:
Assistant
Now let me also write an integration test that exactly mirrors the issue's reproduction. Let me check if there's a pattern for that:
Assistant
Let me create an integration test fixture for this issue. But the integration test fixture pattern is more involved (needs real files). Let me focus on the unit tests I already have, which are sufficient.
Actually, the user mentioned the integration test should be added. But the issue says "No new interfaces are introduced." So the focus should be on the fix and unit tests. The integration test would be a nice addition but is optional.
Let me also look at where I might want to add an integration test. Actually, looking at the test structure, integration tests are for
fallow-core's API. The fix is infallow-graph, so unit tests are more appropriate.Let me also verify my fix works when there's no
namein the canonical package.json (sincepj.name()returnsOption<&str>):Assistant
The package without a name in its package.json works correctly - it's properly tracked as a used dependency. Let me also test the case where the canonical name is
None:Assistant
Let me also restore the original test and confirm everything still works:
Assistant
The fix works correctly. Let me also verify it as a JSON output:
Assistant
All 0 issues, exactly as expected.
Let me now run all tests one final time:
Assistant
Only the pre-existing
fix_batch_aborts_when_a_target_directory_is_read_onlytest fails (unrelated to my change).Let me also run clippy once more:
Assistant
No clippy warnings. Let me also run cargo fmt --check:
Assistant
All formatting checks pass.
Now let me clean up the temporary test scripts:
Assistant
Now let me look at the final diff to ensure everything is good:
Assistant
Now let me also look at the final structure of my changes and verify the integration is correct:
Assistant
All my changes are in place. Let me also do a final test run:
Assistant
All tests pass. Let me also make sure the final binary works correctly:
Assistant
Now let me verify the diff one final time:
Assistant
Let me also examine the changes to make sure the logic is clean:
Assistant
Good. The helper is clean and well-documented.
Now let me think about one more thing - the spec format issue says:
No new interfaces are introduced.
I've added a private helper function
canonical_npm_package_namebut that's internal to the module, not a public interface. So this is fine.Let me make a final summary of the fix and submit:
Assistant
Now let me also verify the final behavior with a comprehensive test:
Assistant
The fix works exactly as expected. All four false positives from the issue are gone.
Let me clean up the temporary files:
Assistant
Now let me address the checklist from the issue:
Checklist
1. If an error was reported in the issue description, I have successfully reproduced it.
YES: I created a reproduction setup that exactly matches the issue:
pnpm-workspace.yamlwith catalog entries@std/csv: jsr:1.0.6andunstorage: npm:unstorage-nightly@2.0.0-alpha.5apps/cli/package.jsondeclaring@std/csv: catalog:apps/native/package.jsondeclaringunstorage: catalog:- TypeScript source files importing from these declared names
- node_modules structure matching what pnpm would create (with symlinks)
The original code produced exactly the false positives described in the issue:
@std/csvreported as unused@jsr/std__csvreported as unlistedunstoragereported as unusedunstorage-nightlyreported as unlisted
2. If an error was reported in the issue description, I have confirmed that my fix resolves the error.
YES: After applying my fix:
total_issues: 0unused_dependencies: []unlisted_dependencies: []
The exact reproduction from the issue now produces no false positives.
3. I have thought about edge cases and made sure my fix handles them as well.
YES: I considered and tested the following edge cases:
-
Normal packages (declared name = canonical name): Uses the path-extracted name, which matches the specifier's name. Same behavior as before.
-
pnpm
jsr:alias (@std/csv→@jsr/std__csv): Correctly credits@std/csv(the declared name). -
pnpm
npm:<name>@<ver>alias (unstorage→unstorage-nightly): Correctly creditsunstorage(the declared name). -
Subpath imports (
@std/csv/stringify): Correctly credits@std/csv(the declared name), not@jsr/std__csv(the resolved name). -
Deep subpath imports (
@std/csv/stringify/utils): Correctly credits@std/csv(the declared name). -
Packages without
namein package.json: Falls back to the path-extracted name (no change from before). -
Resolution failures (no node_modules): Returns the specifier's name as
NpmPackage(no change from before). -
Path that doesn't match standard layout: Returns
Nonefromextract_package_name_from_node_modules_path, which propagates asNonefrom the helper (no change). -
Workspace package self-references: Handled separately by
try_workspace_package_fallbackwithInternalPackageModule, which already uses the specifier's name. No conflict with my fix. -
Mixed scenarios: I tested that when
@std/csvis declared and imported (no alias), no false positives appear. When@std/csvis declared but NOT imported, it's still reported as unused. When something is imported but NOT declared (e.g.,lodashin my test), it's still reported as unlisted.
4. I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass.
YES: I ran the entire test suite (
cargo test --workspace --all-targets):- All 1770 + 2250 + 22 + 6 + 6 + 25 + 7 + 3 + 16 + 4 + 31 = 4140 unit tests pass
- All 482 fallow-graph tests pass (including my 6 new helper tests + 4 new integration tests)
- All 545 fallow-core integration tests pass
The only failing test (
fix_batch_aborts_when_a_target_directory_is_read_only) is a pre-existing failure unrelated to my changes (verified by stashing my changes and seeing the same failure).I also ran
cargo clippy --workspace --all-targets -- -D warnings(no warnings) andcargo fmt --all -- --check(clean).Assistant
The fix is complete. Let me submit:
Assistant
me submit the changes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →