Skip to content

Fix: resolve the browser export condition in core and integrations - #618

Merged
AmaadMartin merged 7 commits into
google:mainfrom
AmaadMartin:fix/core-package-exports-browser-condition
Aug 13, 2026
Merged

AmaadMartin merged 7 commits into
google:mainfrom
AmaadMartin:fix/core-package-exports-browser-condition

Conversation

@AmaadMartin

@AmaadMartin AmaadMartin commented Aug 4, 2026 •

Copy link
Copy Markdown
Collaborator

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

  1. Link to an existing issue (if applicable):
    Closes: package.json exports has no browser condition, so bundlers resolve the Node build and the browser field is ignored #610
    Related: Tracking: @google/adk cannot be bundled for the browser (5 build-level defects) #607

  2. Or, if no issue exists, describe the change:

Problem: core and integrations both declare an exports map with no browser condition. A bundler treats exports as authoritative and stops reading the legacy top-level browser field, so a browser build matches "import" and gets the Node ESM entry. "." is the only declared subpath, so a consumer cannot opt in by hand either. The published dist/web target is therefore unreachable by name in both packages.

Solution: I added a browser condition ahead of import and require, plus a ./dist/web/* subpath, to both manifests. types stays the first condition, so TypeScript resolution does not move. The integrations half is a no-op for consumers today, because its index.ts and index_web.ts both export only version; I changed it anyway so the defect cannot come back silently when the two entries diverge.

Measured, for @google/adk and @google/adk-integrations alike:

resolver before after
webpack 5, target: 'web' dist/esm/index.js dist/web/index_web.js
Vite 5, browser build dist/esm/index.js dist/web/index_web.js
node --conditions=browser dist/esm/index.js dist/web/index_web.js
Node ESM import dist/esm/index.js dist/esm/index.js
Node CJS require dist/cjs/index.js dist/cjs/index.js
<pkg>/dist/web/index_web.js ERR_PACKAGE_PATH_NOT_EXPORTED resolves

The browser surface shrinks, and that is the intent. A browser bundler now gets index_web.js, which is common.js only, instead of the full Node barrel. Nothing that worked stops working: #607 measured 625 errors bundling @google/adk for the browser today, so there is no working browser consumer to break. The exports that disappear are the ones that could never run in a browser, such as GcsArtifactService, DatabaseSessionService and the filesystem skill loaders.

Not blocked on #614, and does not block it. #614 fixes the Node built-ins that still leak into dist/web. This change asserts which file a resolver selects, which only needs that file to exist, so the two are independent and touch disjoint files. One note for whoever lands second: #614 turns dist/web into a single bundled file, after which the ./dist/web/* subpath stays valid but has little left to address, and its "types": "./dist/types/*" mapping becomes approximate.

Changes since the previous revision of this PR. I rebased onto main, applied the same fix to integrations/package.json, and replaced core/test/package_exports_test.ts with a resolution test. That deleted test read the manifest and asserted key order, so it could pass while resolution was broken and could not fail when a renamed dist/web file broke it. Every assertion it made is now covered by resolution instead.

Testing Plan

Please describe the tests that you ran to verify your changes. This is required for all PRs that are not small documentation or typo fixes.

Unit Tests:
[x] I have added or updated unit tests for my change.
[x] All unit tests pass locally.

tests/integration/build_setup/package_resolution_test.ts (10 tests, 1.3s) resolves each package in a child Node process and asserts the path. It runs out of process because every vitest project aliases @google/adk to core/src, so an in-process import never consults the exports map. It also asserts the resolved browser file exists on disk, because import.meta.resolve does not check and a missing target under exports is a hard bundler error.

npx vitest run --project integration tests/integration/build_setup/package_resolution_test.ts
  10 passed (10)

Mutation checks. Each new assertion was run against unfixed code and confirmed to fail.

mutation result
drop the browser condition from core/package.json 1 failed: Expected: .../core/dist/web/index_web.js, Received: .../core/dist/esm/index.js
drop the browser condition from integrations/package.json 1 failed, same message for integrations
drop ./dist/web/* from both manifests 2 failed: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './dist/web/index_web.js' is not defined by "exports"
rename core/dist/web/index_web.js off disk 3 failed, including resolves the browser condition to a file that exists

The last one is the build:bundle case: core/build.js emits dist/web/index.js in --bundle mode, not index_web.js. That is harmless today because prepublishOnly runs the non-bundle build, but after this change it would ship a package that fails to resolve for every browser consumer.

Repo checks on the pushed commit: npm run lint and npm run format:check both pass. npm run ts:check reports 477 pre-existing errors on main and 0 in the new file.

run-tests is green on ubuntu, macOS and windows for the pushed commit. Earlier runs went red on tests/integration/app_loader/app_loader_test.ts, which times out at 40s. That is the pre-existing flake tracked in #622, and a re-run cleared it.

Manual End-to-End (E2E) Tests:
Please provide instructions on how to manually test your changes, including any necessary setup or configuration.

This produced the table above. A stub package copies name, main, module, browser, typings and exports verbatim from each manifest, with each dist entry replaced by a one-line module naming itself. Stubbing isolates resolution from the bundling defects tracked in #607.

npm install webpack@5 webpack-cli vite@5
npx webpack --config webpack.config.cjs   # target: 'web'
npx vite build
grep -o 'dist/[a-z]*' <bundle>            # which dist directory was pulled in

Against the real packages, after npm run build:

echo "import {LlmAgent} from '@google/adk';" > entry.js
npx esbuild --bundle --platform=browser --target=chrome138 --outfile=/dev/null entry.js

esbuild now enters core/dist/web/index_web.js, where before it entered core/dist/esm/index.js. It still reports 4 errors for Node built-ins reached from the web entry. That count is the same before and after, and #614 fixes it.

Checklist

[x] I have read the CONTRIBUTING.md document.
[x] I have performed a self-review of my own code.
[x] I have commented my code, particularly in hard-to-understand areas.
[x] I have added tests that prove my fix is effective or that my feature works.
[x] New and existing unit tests pass locally with my changes.

Comment thread core/package.json Outdated
"types": "./dist/types/*",
"default": "./dist/web/*"
},
"./package.json": "./package.json"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

why do we need this?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good catch — we don't. Removed in 3e5217f, along with the test that covered it.

The exports map predates this PR (it's unchanged context in the hunk), so @google/adk/package.json was already unresolvable on main — require.resolve throws ERR_PACKAGE_PATH_NOT_EXPORTED both before and after the removal, so behaviour now matches main exactly. Nothing in the repo resolves that path, and dev and integrations both declare exports with "." only, so there was no precedent for it either.

The browser condition and the ./dist/web/* subpath are untouched — those are the actual point of the PR.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Still removed after the rebase onto main; the commit is now b8913f5.

Amaad Martin added 6 commits August 7, 2026 10:53
An "exports" map takes precedence over the legacy top-level "browser"
field in every modern bundler, so the field at core/package.json was never
consulted: a browser bundler matched "import" and pulled in the Node build
under dist/esm, along with fs, path, http and zlib.

Declare a "browser" condition on "." -- before "import" and "require", since
conditions match in declaration order and a later one would silently never
fire -- and keep "types" first so TypeScript still finds the declarations.

Because an "exports" map is exhaustive, "." being the only declared subpath
also made the web build unreachable by name. Declare "./dist/web/*" (with a
"types" condition pointing at dist/types, which is where tsc emits the
declarations) and re-expose "./package.json".

Node ESM, Node CJS and TypeScript resolution are unchanged.

Fixes #610
Guards the invariant a future edit can silently break: the "browser"
condition must sit after "types" and before "import" and "require", because
conditions match in declaration order and a misplaced one is a no-op that
still looks correct in review.
Assert the condition map's value before its key order, so deleting the
subpath reports the expected object rather than a TypeError from
Object.keys(undefined).
Reviewer asked why it is needed. It is not: core/package.json already
declared an "exports" map before this change, so the encapsulation boundary
that makes @google/adk/package.json unresolvable predates this PR rather
than being introduced by it. Nothing in the repo resolves that specifier,
and dev/ and integrations/ declare "." alone, so there is no precedent
either. Removing it leaves that specifier exactly as it behaves on main.

Drops the accompanying test with it: it only asserted the entry this change
had added, so it pinned nothing once the entry is gone.
…xports

The exports map shadows the legacy top-level browser field, so a browser
bundler resolving @google/adk-integrations lands on the Node ESM build and
dist/web is unreachable by name. Mirrors the core manifest.
Replaces the shape-only core/test/package_exports_test.ts, which read the
manifest and asserted key order. That test can pass while resolution is
broken, and cannot fail when a renamed dist/web file breaks it.

The new test spawns Node with an explicit condition set, because every
vitest project aliases @google/adk to core/src and would bypass the
exports map entirely.
@AmaadMartin
AmaadMartin force-pushed the fix/core-package-exports-browser-condition branch from 3e5217f to 5fb12da Compare August 7, 2026 18:05
@AmaadMartin AmaadMartin changed the title Fix: add browser condition and web subpath to core package exports Fix: resolve the browser export condition in core and integrations Aug 7, 2026
Keeps the helper out of the describe closure, so it takes the package
directory as a parameter like the other helpers in this file.
@AmaadMartin
AmaadMartin merged commit 0f9f73d into google:main Aug 13, 2026
13 of 15 checks passed
@kalenkevich kalenkevich mentioned this pull request Aug 13, 2026
prasanna8585 pushed a commit to prasanna8585/adk-js that referenced this pull request Aug 21, 2026
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.

package.json exports has no browser condition, so bundlers resolve the Node build and the browser field is ignored

2 participants