Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "none",
"comment": "chore(react-text): add an opt-in build-independent dependency lint pilot",
"packageName": "@fluentui/react-text",
"email": "paulmardling@microsoft.com"
}
24 changes: 24 additions & 0 deletions packages/react-components/react-text/library/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,27 @@ For more information about the components, please refer to the [API documentatio
## Migration

For migration information, have a look at the [migration guide](./MIGRATION.md).

## Dependency lint pilot

From the repository root, run `yarn nx run react-text:lint-dependencies` to check that production source dependencies
are declared in this package's `dependencies`, `peerDependencies`, or `optionalDependencies`. This opt-in Nx
manifest check does not build component packages. Run it through Nx so that its source dependency graph and file map
are refreshed first; missing graph data is an error, not a successful check.

The pilot excludes test files through the build's production inputs, and excludes configuration and `src/testing`
helpers explicitly. It accounts for generated external SWC helpers, but Nx does not infer the workspace JSX runtime
dependency from `@jsxImportSource` pragmas. Obsolete dependencies and version ranges are not checked. Nx source
analysis also includes type-only imports, so this is not yet a drop-in replacement for
`import/no-extraneous-dependencies`. Keep the JSX runtime declaration when using the corresponding pragma.

This check only covers imports represented in Nx's dependency graph; it does not validate unresolved imports.
The shared `import/no-unresolved` rule is disabled, so retain normal type-check validation rather than relying
on this manifest check to catch unresolved imports.

The existing source lint rule and workspace build prerequisites are unchanged. This pilot builds on
[#32338](https://github.com/microsoft/fluentui/pull/32338); v8 support and broader rollout for
[#27727](https://github.com/microsoft/fluentui/issues/27727) remain separate work.

Run the focused regression suite with
`yarn nx run react-text:test --runInBand --runTestsByPath config/dependency-lint.test.js`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/** @jest-environment node */

const { Linter } = require('eslint');
const importPlugin = require('eslint-plugin-import');
const { readCachedProjectGraph, workspaceRoot } = require('@nx/devkit');
const { readFileMapCache } = require('nx/src/project-graph/nx-deps-cache');
const { createProjectRootMappings } = require('nx/src/project-graph/utils/find-project-for-path');
const { join, relative } = require('node:path');
const { mkdtempSync, writeFileSync, mkdirSync, rmSync } = require('node:fs');
const { tmpdir } = require('node:os');
const { spawnSync } = require('node:child_process');

const config = require('./eslint.config.cjs');
const manifest = require('../package.json');
const projectRoot = 'packages/react-components/react-text/library';

describe('dependency lint pilot', () => {
const graphGlobals = ['projectGraph', 'projectFileMap', 'projectRootMappings', 'targetProjectLocator'];
let originalGlobals;
let graph;
let fileMap;

beforeEach(() => {
originalGlobals = Object.fromEntries(graphGlobals.map(key => [key, globalThis[key]]));
graph = JSON.parse(JSON.stringify(readCachedProjectGraph()));
fileMap = JSON.parse(JSON.stringify(readFileMapCache().fileMap.projectFileMap));
globalThis.projectGraph = graph;
globalThis.projectFileMap = fileMap;
globalThis.projectRootMappings = createProjectRootMappings(graph.nodes);
});

afterEach(() => {
for (const key of graphGlobals) {
if (originalGlobals[key] === undefined) {
delete globalThis[key];
} else {
globalThis[key] = originalGlobals[key];
}
}
});

function lint(packageJson) {
return new Linter().verify(JSON.stringify(packageJson), config, {
filename: join(workspaceRoot, projectRoot, 'package.json'),
});
}

function addImport(file, dependency) {
fileMap['react-text'].push({ file: `${projectRoot}/${file}`, hash: 'fixture', deps: [dependency] });
}

it('accepts the current manifest without building component packages', () => {
expect(lint(manifest)).toEqual([]);
});

it.each(['@fluentui/react-theme', '@griffel/react', '@swc/helpers'])(
'reports a missing production dependency: %s',
dependency => {
const packageJson = JSON.parse(JSON.stringify(manifest));
delete packageJson.dependencies[dependency];
const messages = lint(packageJson);
expect(messages.length).toBeGreaterThan(0);
expect(messages.every(message => message.ruleId === '@nx/dependency-checks')).toBe(true);
expect(messages.some(message => message.message.includes(dependency))).toBe(true);
},
);

describe.each(['@fluentui/react-theme', '@griffel/react'])('%s declarations', dependency => {
it('does not accept a runtime dependency declared only as a devDependency', () => {
const packageJson = JSON.parse(JSON.stringify(manifest));
packageJson.devDependencies = {
...packageJson.devDependencies,
[dependency]: packageJson.dependencies[dependency],
};
delete packageJson.dependencies[dependency];
expect(lint(packageJson).some(message => message.message.includes(dependency))).toBe(true);
});

it.each(['peerDependencies', 'optionalDependencies'])('accepts a runtime declaration in %s', section => {
const packageJson = JSON.parse(JSON.stringify(manifest));
packageJson[section] = {
...packageJson[section],
[dependency]: packageJson.dependencies[dependency],
};
delete packageJson.dependencies[dependency];
expect(lint(packageJson)).toEqual([]);
});
});

it('does not exempt external runtime imports installed as root devDependencies', () => {
addImport('src/runtime.ts', 'npm:lodash');
expect(lint(manifest).some(message => message.message.includes('lodash'))).toBe(true);
});

it.each(['src/fixture.test.ts', 'src/testing/fixture.ts', 'config/fixture.cjs'])(
'ignores development imports from %s',
file => {
addImport(file, 'npm:lodash');
expect(lint(manifest)).toEqual([]);
},
);

it('does not introduce obsolete dependency or version-range enforcement', () => {
const packageJson = JSON.parse(JSON.stringify(manifest));
packageJson.dependencies.lodash = '0.0.0';
packageJson.dependencies['@griffel/react'] = '0.0.0';
expect(lint(packageJson)).toEqual([]);
});

it('records the unsupported generated workspace JSX runtime case', () => {
const packageJson = JSON.parse(JSON.stringify(manifest));
delete packageJson.dependencies['@fluentui/react-jsx-runtime'];
expect(lint(packageJson)).toEqual([]);
});

it('reports the same missing workspace dependency before and after its entry point exists', () => {
const directory = mkdtempSync(join(tmpdir(), 'react-text-dependency-lint-'));
try {
const dependencyRoot = join(directory, 'node_modules/@fixture/dependency');
mkdirSync(dependencyRoot, { recursive: true });
mkdirSync(join(directory, 'src'));
writeFileSync(join(directory, 'package.json'), JSON.stringify({ name: '@fixture/consumer', version: '1.0.0' }));
writeFileSync(
join(dependencyRoot, 'package.json'),
JSON.stringify({ name: '@fixture/dependency', version: '1.0.0', main: 'lib/index.js' }),
);
graph.nodes['dependency-lint-fixture'] = {
name: 'dependency-lint-fixture',
type: 'lib',
data: { root: relative(workspaceRoot, dependencyRoot), targets: { build: { executor: 'nx:noop' } } },
};
addImport('src/runtime.ts', 'dependency-lint-fixture');
const lintImport = () =>
new Linter({ cwd: directory }).verify(
'import { value } from "@fixture/dependency";',
[
{
plugins: { import: importPlugin },
rules: { 'import/no-extraneous-dependencies': ['error', { packageDir: directory }] },
},
],
{ filename: join(directory, 'src/index.js') },
);
expect(lintImport()).toEqual([]);
const before = lint(manifest);
expect(before.some(message => message.message.includes('@fixture/dependency'))).toBe(true);

mkdirSync(join(dependencyRoot, 'lib'));
writeFileSync(join(dependencyRoot, 'lib/index.js'), 'exports.value = 1;');
expect(lintImport().map(message => message.ruleId)).toEqual(['import/no-extraneous-dependencies']);
expect(lint(manifest)).toEqual(before);
expect(lint({ ...manifest, dependencies: { ...manifest.dependencies, '@fixture/dependency': '1.0.0' } })).toEqual(
[],
);
} finally {
rmSync(directory, { recursive: true, force: true });
}
});

it('fails instead of skipping the check when the Nx graph is missing', () => {
const directory = mkdtempSync(join(tmpdir(), 'react-text-dependency-graph-'));
try {
const result = spawnSync(process.execPath, ['-e', 'require("./config/eslint.config.cjs")'], {
cwd: join(workspaceRoot, projectRoot),
env: { ...process.env, NX_WORKSPACE_DATA_DIRECTORY: directory },
encoding: 'utf8',
});
expect(result.error).toBeUndefined();
expect(result.status).not.toBe(0);
expect(result.stderr).toContain('No cached ProjectGraph is available');
} finally {
rmSync(directory, { recursive: true, force: true });
}
});

it.each(['project', 'build target', 'file map'])('fails when the cached %s is missing', missing => {
const directory = mkdtempSync(join(tmpdir(), 'react-text-dependency-graph-'));
try {
if (missing === 'project') {
delete graph.nodes['react-text'];
} else if (missing === 'build target') {
delete graph.nodes['react-text'].data.targets.build;
}
writeFileSync(join(directory, 'project-graph.json'), JSON.stringify(graph));
if (missing !== 'file map') {
writeFileSync(join(directory, 'file-map.json'), JSON.stringify({ fileMap: { projectFileMap: fileMap } }));
}
const result = spawnSync(process.execPath, ['-e', 'require("./config/eslint.config.cjs")'], {
cwd: join(workspaceRoot, projectRoot),
env: { ...process.env, NX_WORKSPACE_DATA_DIRECTORY: directory },
encoding: 'utf8',
});
expect(result.error).toBeUndefined();
expect(result.status).not.toBe(0);
expect(result.stderr).toContain('Dependency lint requires the react-text project graph and file map');
} finally {
rmSync(directory, { recursive: true, force: true });
}
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// @ts-check

const { readCachedProjectGraph } = require('@nx/devkit');
const nxPlugin = require('@nx/eslint-plugin');
const jsonParser = require('jsonc-eslint-parser');
const { readFileMapCache } = require('nx/src/project-graph/nx-deps-cache');

// Nx otherwise warns and skips dependency checking when its graph cache is missing.
const graph = readCachedProjectGraph();
if (!graph.nodes['react-text']?.data.targets?.build || !readFileMapCache()?.fileMap.projectFileMap['react-text']) {
throw new Error(
'Dependency lint requires the react-text project graph and file map. Run yarn nx run react-text:lint-dependencies.',
Comment thread
PaulGMardling marked this conversation as resolved.
Resolved
Comment thread
PaulGMardling marked this conversation as resolved.
Resolved
);
}
Comment thread
PaulGMardling marked this conversation as resolved.
Resolved

module.exports = [
{
files: ['**/package.json'],
languageOptions: { parser: jsonParser },
plugins: { '@nx': nxPlugin },
rules: {
'@nx/dependency-checks': [
'error',
{
checkObsoleteDependencies: false,
checkVersionMismatches: false,
ignoredFiles: ['{projectRoot}/config/**', '{projectRoot}/src/testing/**'],
runtimeHelpers: ['@swc/helpers'],
},
],
},
},
];
21 changes: 21 additions & 0 deletions packages/react-components/react-text/library/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
"$schema": "../../../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"sourceRoot": "packages/react-components/react-text/library/src",
"targets": {
"lint-dependencies": {
"executor": "@nx/eslint:lint",
"dependsOn": [],
"cache": true,
"inputs": [
"default",
"^production",
"{workspaceRoot}/nx.json",
"{workspaceRoot}/tsconfig.base.json",
"{workspaceRoot}/package.json",
"{workspaceRoot}/yarn.lock",
{ "externalDependencies": ["@nx/eslint", "@nx/eslint-plugin", "jsonc-eslint-parser", "eslint"] }
],
"options": {
"eslintConfig": "packages/react-components/react-text/library/config/eslint.config.cjs",
"lintFilePatterns": ["{projectRoot}/package.json"],
"errorOnUnmatchedPattern": true
}
}
},
"tags": ["vNext", "platform:web"],
"implicitDependencies": []
}
Loading