Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/evidence/P2_CONDITIONAL_EXPORTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# P2 — conditional package root exports: source-proven or UNKNOWN

Upstream evidence (existing previously exposed corpus; P7 remains untouched):
- TanStack Form exact source SHA 5de1a8de01e3d63a0e5225a7e659a384f3a0effb; packages/react-form/package.json exports root dist/esm and dist/cjs, not source index.
- React Hook Form exact source SHA 8d7bd6c2c774fd9b4310e8cbc07d36ce1cacf736; package.json exports dist-only root import/require/type targets.

Pre-fix source-reduced test-only PR #21 SHA 2f46e5cb93634773a1050b41cf2f2138820acdb4 reproduced four distinct FAIL in [original Ubuntu Node22 push job](https://github.com/levencode-labs/blastradius/actions/runs/45033942120) 137243402333: TanStack Form dist-only root, React Hook Form dist-only root, import:null versus default, and types-only source pretending to be runtime. Of 174 tests, 170 PASS, 4 FAIL. Two source-exact/legacy positive controls passed. Original CI logs preserved.

Bounded fix: explicit exports map is authoritative for root and subpath; require source-exact import/default runtime branch. Explicit import:null blocks fallback; types alone cannot prove runtime binding. The legacy root source index fallback is kept only if there is no exports map. Never guess dist-to-src or package condition environments; unsupported type-only/runtime distinctions remain UNKNOWN. Cache engine model version advanced; npm 0.0.1-dev remains private.

Acceptance requires all eight original OS/Node 22/24 CI cells, tarball byte parity, original nine independent pinned source TypeChecker/Knip, and actual merge SHA. No release/tag/publish/auto-delete.
43 changes: 27 additions & 16 deletions packages/core/src/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,36 @@ export function buildGraph({root,files,config={},reuseFacts,saveFacts,extraDiagn
const name=[...workspacePackages.keys()].sort((a,b)=>b.length-a.length).find(n=>specifier===n||specifier.startsWith(n+'/'));
if(name){
const pkg=workspacePackages.get(name)!;const sub=specifier===name?'.':'./'+specifier.slice(name.length+1);
const exports=pkg.manifest.exports;let entry:unknown;
const exports=pkg.manifest.exports,hasExportMap=exports!==undefined;
let entry:unknown;
if(sub==='.'&&typeof exports==='string')entry=exports;
else if(exports&&typeof exports==='object')entry=(exports as Record<string,unknown>)[sub];
const prefer=(e:unknown):string|undefined=>typeof e==='string'?e:e&&typeof e==='object'?(prefer((e as Record<string,unknown>).import)||prefer((e as Record<string,unknown>).default)||prefer((e as Record<string,unknown>).types)):undefined;
// An explicit package "exports" map is authoritative for subpaths.
// Never turn an unlisted/blocked private import into a *verified*
// edge just because ./src/private.ts or ./private.ts exists in the
// workspace (TanStack Table). Do not guess dist->src equivalence.
const hasExportMap=exports!==undefined;
const rejectedSubpath=sub!=='.'&&hasExportMap&&(entry===undefined||entry===null);
if(rejectedSubpath){
diag('MODULE_RESOLUTION_FAILED',source,1,1,'Workspace package subpath lacks a proven supported export: '+specifier);
else if(exports&&typeof exports==='object'&&!Array.isArray(exports)){
const map=exports as Record<string,unknown>;
if(Object.prototype.hasOwnProperty.call(map,sub))entry=map[sub];
// A root-only conditional export map is also legal.
else if(sub==='.'&&Object.keys(map).every(k=>!k.startsWith('.')))entry=map;
}
// Import/default runtime conditions alone can authorize a verified
// source mapping. A present null import condition is a hard block.
// Declaration-only types never prove a runtime import.
const prefer=(e:unknown):string|undefined=>{
if(typeof e==='string')return e;
if(!e||typeof e!=='object'||Array.isArray(e))return undefined;
const c=e as Record<string,unknown>;
if(Object.prototype.hasOwnProperty.call(c,'import'))return prefer(c.import);
if(Object.prototype.hasOwnProperty.call(c,'default'))return prefer(c.default);
return undefined;
};
// Explicit package exports are authoritative for root and subpaths.
// Never infer dist-to-src, nor fall back from blocked paths.
const blocked=hasExportMap&&(entry===undefined||entry===null||!prefer(entry));
if(blocked){
diag('MODULE_RESOLUTION_FAILED',source,1,1,'Workspace package has no proven runtime source export: '+specifier);
}else{
// Legacy deep-source fallback is allowed only when the package has
// no explicit export map. An explicitly exported source subpath
// still resolves when its exact exported target is in the snapshot.
const fallback=sub==='.'
// Legacy source fallback exists only without an explicit exports map.
const fallback=hasExportMap?[]:sub==='.'
?[pkg.manifest.module,pkg.manifest.main,pkg.manifest.types,'./src/index.ts','./index.ts']
:hasExportMap?[]:[sub];
:[sub];
const candidates=[prefer(entry),...fallback].filter((x):x is string=>typeof x==='string');
for(const candidate of candidates){const base=path.posix.normalize(path.posix.join(pkg.dir,candidate));
const possibilities=[base,base+'.ts',base+'.tsx',base+'.js',base+'.jsx',base+'/index.ts',base+'/index.tsx'];
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const ENGINE_VERSION = '0.0.12-dev';
export const ENGINE_VERSION = '0.0.13-dev';
export type NodeKind = 'FILE' | 'MODULE_EVAL' | 'SYMBOL' | 'EXPORT_BINDING' | 'ROUTE' | 'TEST_ENTRY' | 'STORY_ENTRY' | 'PACKAGE_API' | 'UNKNOWN_ENTRY' | 'EXTERNAL';
export type RootKind = 'production' | 'test' | 'story' | 'publicApi';
export type EdgeKind = 'imports' | 'references' | 'calls' | 'renders' | 'reexports' | 'type_references' | 'dynamic_import' | 'side_effect_import' | 'prop_access' | 'prop_pass' | 'route_entry' | 'test_dependency' | 'public_export' | 'possible_dynamic';
Expand Down
76 changes: 76 additions & 0 deletions tests/p2-conditional-root-exports.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import {buildGraph} from '../dist/packages/core/src/index.js';

// Source-grounded: TanStack Form react-form package@5de1a8de01e3d63a0e5225a7e659a384f3a0effb
// declares "." as import:{types:"./dist/esm/index.d.ts",default:"./dist/esm/index.js"}
// and require:{types:"./dist/cjs/index.d.cts",default:"./dist/cjs/index.cjs"}.
// There is a local src/index.ts but no proven dist->src equivalence.
// React Hook Form@8d7bd6c... likewise exports dist-only public entry points.
// Resolver must NOT fabricate a verified root import via ./src/index.ts fallback.
const consumer='apps/site/src/main.tsx';
const target='packages/react-form/src/index.ts';
function scenario(t,manifest){
const root=fs.mkdtempSync(path.join(os.tmpdir(),'br-p2-conditional-'));
t.after(()=>fs.rmSync(root,{recursive:true,force:true,maxRetries:5,retryDelay:100}));
const files=new Map([
['package.json',JSON.stringify({private:true,workspaces:['packages/*']})],
['packages/react-form/package.json',JSON.stringify({name:'@tanstack/react-form',...manifest})],
[target,"export const useForm=()=>({valid:true});"],
[consumer,"import {useForm} from '@tanstack/react-form'; export const value=useForm();"]
]);
return buildGraph({root,files});
}
const edge=g=>g.edges.some(e=>e.sourceFile===consumer&&e.to==='export:'+target+'#useForm'&&
['references','renders','type_references'].includes(e.kind)&&e.certainty==='verified');

test('real TanStack Form dist-only conditional exports never prove local source equivalence',t=>{
const g=scenario(t,{
module:'./dist/esm/index.js',main:'./dist/cjs/index.cjs',types:'./dist/esm/index.d.ts',
exports:{'.':{
import:{types:'./dist/esm/index.d.ts',default:'./dist/esm/index.js'},
require:{types:'./dist/cjs/index.d.cts',default:'./dist/cjs/index.cjs'}
}}
});
assert.equal(edge(g),false,'unproven dist->src mapping must remain UNKNOWN, not a verified static edge');
assert.ok(g.diagnostics.some(d=>d.code==='MODULE_RESOLUTION_FAILED'),'source mapping unavailable must be diagnosed');
});

test('real React Hook Form dist-only exports must not fall through to local source barrel',t=>{
const g=scenario(t,{
main:'./dist/index.cjs.js',module:'./dist/index.esm.mjs',types:'./dist/index.d.ts',
exports:{'.':{types:'./dist/index.d.ts','react-server':'./dist/react-server.esm.mjs',
import:'./dist/index.esm.mjs',require:'./dist/index.cjs.js'}}
});
assert.equal(edge(g),false);
assert.ok(g.diagnostics.some(d=>d.code==='MODULE_RESOLUTION_FAILED'));
});

test('negative: import:null condition blocks verified fallback to default source',t=>{
const g=scenario(t,{
exports:{'.':{import:null,default:'./src/index.ts'}}
});
assert.equal(edge(g),false,'explicit blocked import condition must not choose default');
assert.ok(g.diagnostics.some(d=>d.code==='MODULE_RESOLUTION_FAILED'));
});

test('negative: type-only export does not authorize a runtime import edge',t=>{
const g=scenario(t,{
exports:{'.':{types:'./src/index.ts',import:null}}
});
assert.equal(edge(g),false);
assert.ok(g.diagnostics.some(d=>d.code==='MODULE_RESOLUTION_FAILED'));
});

test('positive: explicit root import condition pointing to exact existing source remains proven',t=>{
const g=scenario(t,{exports:{'.':{import:{default:'./src/index.ts'}}}});
assert.equal(edge(g),true);
});

test('positive: legacy package with no exports map retains old source main fallback',t=>{
const g=scenario(t,{main:'./src/index.ts'});
assert.equal(edge(g),true);
});
Loading