Skip to content

Fix saved package export artifact imports - #307

Merged
kentcdodds merged 3 commits into
mainfrom
cursor/kody-saved-package-dependencies-75c5
May 1, 2026
Merged

kentcdodds merged 3 commits into
mainfrom
cursor/kody-saved-package-dependencies-75c5

Conversation

@kentcdodds

@kentcdodds kentcdodds commented May 1, 2026 •

Copy link
Copy Markdown
Member

Summary

  • make kody:@scope/package/export imports prefer published module bundle artifacts during execute/runtime composition
  • keep raw published source as a fallback only when it can rebuild without ad hoc dependency installs, preserving the republish guidance for dependency-backed packages
  • address review feedback by reusing the manifest export-key normalizer, preserving packageName when dependency records are replayed, and switching the test mock to a top-level type import
  • replace lossy sanitized export/specifier path keys with collision-safe encoded keys for published artifact materialization and proxy modules so distinct exports cannot overwrite each other

Testing

  • npx vitest run --config vitest.node.config.ts packages/worker/src/repo/checks.node.test.ts packages/worker/src/package-runtime/module-graph.node.test.ts packages/worker/src/package-runtime/published-bundle-artifacts.node.test.ts
  • npx vitest run --config vitest.workers.config.ts packages/worker/src/package-runtime/module-graph.workers.test.ts
  • git push -u origin cursor/kody-saved-package-dependencies-75c5 (pre-push hook ran npm run test && npm run test:e2e:run successfully)
  • Validation log artifact: saved_package_dependency_test_log.txt
Open in Web Open in Cursor 

Summary by CodeRabbit

  • New Features

    • Compose imports from pre-published module artifacts and fall back to original package exports when artifacts aren’t available.
    • Expose a package export normalization helper for reuse across the runtime.
  • Tests

    • Added coverage for embedding published bundles, dependency tracking, and explicit error behavior when artifacts are unavailable.
    • Adjusted test timeouts and assertions to improve reliability.

@coderabbitai

coderabbitai Bot commented May 1, 2026 •

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

The module graph now prefers composing imports from pre-published module artifacts for saved packages with published_commit: it attempts to load a published artifact, materializes artifact modules into state and routes resolution to the artifact's main module, with fallback assertions and dependency tracking that include packageName.

Changes

Cohort / File(s) Summary
Module graph: published-artifact resolution
packages/worker/src/package-runtime/module-graph.ts
Detects published_commit saved packages, calls loadPublishedBundleArtifactByIdentity, materializes artifact.modules into state.files under .__published_bundle__/<export>, sets the artifact mainModule as proxy target, and records/merges artifact dependencies (now carrying packageName). Falls back to standard export resolution after asserting published source can rebuild.
Node tests for published bundles
packages/worker/src/package-runtime/module-graph.node.test.ts
Adds mock-forwarding of published-bundle-artifacts with an interceptable mockModule for loadPublishedBundleArtifactByIdentity; resets mock between tests. Adds tests verifying embedding of .__published_bundle__ content when artifacts match and error rejection when artifact is missing for npm-dependent exports. Updates expected dependency objects to include packageName.
Worker tests timeout change
packages/worker/src/package-runtime/module-graph.workers.test.ts
Refactors tests to use test(name, { timeout: 20_000 }, async () => ...) to set a 20s timeout while preserving existing bundling, execution, and assertions.
Manifest helper export
packages/worker/src/package-registry/manifest.ts
Makes normalizePackageExportKey exported so other modules (module-graph) can reuse it.

Sequence Diagram

sequenceDiagram
    participant ModuleGraph as Module Graph
    participant ArtifactLoader as Artifact Loader
    participant State as State Manager
    participant Resolver as Package Resolver

    ModuleGraph->>ModuleGraph: detect saved_package.published_commit?
    alt published_commit present
        ModuleGraph->>ArtifactLoader: loadPublishedBundleArtifactByIdentity(identity)
        activate ArtifactLoader
        ArtifactLoader-->>ModuleGraph: artifact (or null)
        deactivate ArtifactLoader
        alt artifact found
            ModuleGraph->>State: materialize artifact.modules into\n'__published_bundle__/<export>'
            ModuleGraph->>State: record/merge artifact dependencies\n(include packageName via composite key)
            ModuleGraph->>ModuleGraph: set proxy target = artifact.mainModule
        else artifact null
            ModuleGraph->>Resolver: assertPublishedSourceCanRebuildWithoutDeps()
            Resolver-->>ModuleGraph: assertion result
            alt assertion passes
                ModuleGraph->>ModuleGraph: resolve export via\nresolvePackageExportPath + loaded.prefix
            else assertion fails
                ModuleGraph-->>ModuleGraph: reject with error
            end
        end
    else not published_commit
        ModuleGraph->>ModuleGraph: standard resolution path
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Poem

🐰 I dug a tunnel to artifact lands,

bundles snug in carroted hands,
Embeds stitched from a published seam,
Exports hopping into the stream—
Hooray, the graph now hums and stands!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Fix saved package export artifact imports' directly describes the main purpose of the changeset: fixing how saved package export artifacts are imported during runtime, which is reflected across all modified files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch cursor/kody-saved-package-dependencies-75c5

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@kentcdodds
kentcdodds marked this pull request as ready for review May 1, 2026 05:10
@github-actions

github-actions Bot commented May 1, 2026 •

Copy link
Copy Markdown
Contributor

🔎 Preview deployed: https://kody-pr-307.kentcdodds.workers.dev

Worker: kody-pr-307
D1: kody-pr-307-db
KV: kody-pr-307-oauth-kv

Mocks:

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/worker/src/package-runtime/module-graph.node.test.ts`:
- Around line 28-31: Replace the inline typeof import(...) generic with a named
type import: add a type-only import statement such as "import type * as
PublishedBundleArtifacts from './published-bundle-artifacts.ts'" near the top of
the test and then change the vi.importActual generic to use "typeof
PublishedBundleArtifacts" instead of "typeof
import('./published-bundle-artifacts.ts')"; update the vi.mock block that calls
vi.importActual to reference that named type so the mock behavior is unchanged
and the consistent-type-imports lint rule is satisfied.

In `@packages/worker/src/package-runtime/module-graph.ts`:
- Around line 106-110: The rememberDependency function currently blindly
overwrites state.dependencies with the incoming BundleArtifactDependency, which
can drop an existing packageName; modify rememberDependency to first lookup
state.dependencies.get(createDependencyKey(dependency)) and if an existing
dependency has a packageName but the incoming dependency.packageName is
undefined, copy or merge the existing.packageName into the incoming record (or
keep the existing object) before calling state.dependencies.set so package
identity recorded by ensurePackageLoaded is preserved; ensure you reference the
existing entry from state.dependencies and only override fields that are
intentionally provided by the new dependency.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 752a0bd3-a1af-40a1-9c91-6257b16c2ba3

📥 Commits

Reviewing files that changed from the base of the PR and between 320ddf8 and 372c2a5.

📒 Files selected for processing (3)
  • packages/worker/src/package-runtime/module-graph.node.test.ts
  • packages/worker/src/package-runtime/module-graph.ts
  • packages/worker/src/package-runtime/module-graph.workers.test.ts

Comment thread packages/worker/src/package-runtime/module-graph.node.test.ts Outdated
Comment thread packages/worker/src/package-runtime/module-graph.ts Outdated

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 372c2a5. Configure here.

Comment thread packages/worker/src/package-runtime/module-graph.ts

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/worker/src/package-runtime/module-graph.ts`:
- Around line 338-352: The current use of sanitizeSpecifier(exportName) to build
artifactPrefix is lossy and can collide (e.g., "./foo.bar" vs "./foo-bar");
change the key used in artifactPrefix to a collision-free representation by
computing a stable encoded or hashed form of the normalized export key (e.g.,
SHA256/base64 or hex) instead of sanitizeSpecifier(exportName). Update the logic
that builds artifactPrefix (where joinPath(input.loaded.prefix,
'.__published_bundle__', sanitizeSpecifier(exportName)) is used) to replace
sanitizeSpecifier(exportName) with the new hash/encoding function applied to
exportName (or its normalized form), ensuring materializeArtifactModuleSource
and the returned main module path still use the new artifactPrefix consistently
so multiple distinct exports can be materialized without overwriting each other.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 2f383cc3-9966-4e74-8404-64d9312ccc38

📥 Commits

Reviewing files that changed from the base of the PR and between 372c2a5 and 495e4fe.

📒 Files selected for processing (3)
  • packages/worker/src/package-registry/manifest.ts
  • packages/worker/src/package-runtime/module-graph.node.test.ts
  • packages/worker/src/package-runtime/module-graph.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/worker/src/package-runtime/module-graph.node.test.ts

Comment thread packages/worker/src/package-runtime/module-graph.ts
@kentcdodds
kentcdodds merged commit cd79b18 into main May 1, 2026
9 checks passed
@kentcdodds
kentcdodds deleted the cursor/kody-saved-package-dependencies-75c5 branch May 1, 2026 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants