@smoothbricks/nx-plugin 0.3.13 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,8 @@
1
1
  import { spawn } from 'node:child_process';
2
2
  import { randomUUID } from 'node:crypto';
3
3
  import { once } from 'node:events';
4
- import { rm, writeFile } from 'node:fs/promises';
5
- import { basename, dirname, isAbsolute, join } from 'node:path';
4
+ import { chmod, rm, stat, writeFile } from 'node:fs/promises';
5
+ import { basename, dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
6
6
 
7
7
  import { readJsonFile } from 'nx/src/devkit-exports.js';
8
8
 
@@ -67,6 +67,8 @@ export async function runTypeScriptEmit(
67
67
  return { success: false };
68
68
  }
69
69
 
70
+ await makePackageBinsExecutable(cwd, options.executableOutputs ?? []);
71
+
70
72
  const declarationsSucceeded = await runner({
71
73
  command: 'tsc',
72
74
  args: ['-p', overlayPath, '--emitDeclarationOnly', '--declaration', '--declarationMap'],
@@ -78,6 +80,26 @@ export async function runTypeScriptEmit(
78
80
  }
79
81
  }
80
82
 
83
+ async function makePackageBinsExecutable(cwd: string, outputs: readonly string[]): Promise<void> {
84
+ for (const output of new Set(outputs)) {
85
+ const outputPath = resolve(cwd, output);
86
+ const projectRelative = relative(cwd, outputPath);
87
+ if (
88
+ projectRelative.length === 0 ||
89
+ projectRelative === '..' ||
90
+ projectRelative.startsWith(`..${sep}`) ||
91
+ isAbsolute(projectRelative)
92
+ ) {
93
+ throw new Error(`Executable output must stay inside the project: ${output}`);
94
+ }
95
+ const outputStat = await stat(outputPath);
96
+ // TypeScript creates emitted files with the process default mode. A package
97
+ // bin that loses its execute bits is present but unusable through
98
+ // node_modules/.bin, so restore the semantic guarantee declared by `bin`.
99
+ await chmod(outputPath, outputStat.mode | 0o111);
100
+ }
101
+ }
102
+
81
103
  async function runCompiler(invocation: CompilerInvocation): Promise<boolean> {
82
104
  const child = spawn(invocation.command, invocation.args, {
83
105
  cwd: invocation.cwd,
@@ -5,6 +5,14 @@
5
5
  "description": "Emit transformer-generated JavaScript with ttsc and declarations with native tsc",
6
6
  "type": "object",
7
7
  "properties": {
8
+ "executableOutputs": {
9
+ "type": "array",
10
+ "description": "Package bin outputs to make executable after JavaScript emit",
11
+ "items": {
12
+ "type": "string"
13
+ },
14
+ "uniqueItems": true
15
+ },
8
16
  "cwd": {
9
17
  "type": "string",
10
18
  "description": "Working directory, relative to the workspace root"
@@ -1,4 +1,5 @@
1
1
  export interface TypeScriptEmitOptions {
2
+ executableOutputs?: string[];
2
3
  cwd: string;
3
4
  tsConfig: string;
4
5
  }
package/src/index.test.ts CHANGED
@@ -62,12 +62,40 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
62
62
  }
63
63
  });
64
64
 
65
+ it('never infers a project from a package.json a build wrote', async () => {
66
+ const workspace = await createWorkspace();
67
+ try {
68
+ await workspace.write('packages/cowshed/package.json', '{"name":"cowshed","private":true}\n');
69
+ // napi's platform build, a dist tree, cargo's target dir, a package cache: every
70
+ // one of these is written by a task, and a graph recomputed while it is being
71
+ // written must not see a second `cowshed` and drop the real one.
72
+ const outputs = [
73
+ 'packages/cowshed/.cache/native-debug/package.json',
74
+ 'packages/cowshed/dist/npm/linux-x64-gnu/package.json',
75
+ 'packages/cowshed/target/debug/build/x/out/package.json',
76
+ 'packages/cowshed/node_modules/dep/package.json',
77
+ ];
78
+ for (const path of outputs) {
79
+ await workspace.write(path, '{"name":"cowshed","private":true}\n');
80
+ }
81
+
82
+ const inferred = await inferTargets(outputs, undefined, workspace.context);
83
+ for (const [, result] of inferred) {
84
+ expect(result).toEqual({});
85
+ }
86
+ const live = await inferProject(workspace, 'packages/cowshed/package.json');
87
+ expect(live?.name).toBe('cowshed');
88
+ } finally {
89
+ await workspace.cleanup();
90
+ }
91
+ });
92
+
65
93
  it('splits transformed JavaScript emit from native declarations and typechecking', async () => {
66
94
  const workspace = await createWorkspace();
67
95
  try {
68
96
  await workspace.write(
69
97
  'packages/example/package.json',
70
- '{"name":"example","scripts":{"test":"bun test --pass-with-no-tests"}}\n',
98
+ '{"name":"example","bin":{"example":"./dist/bin/example.js"},"scripts":{"test":"bun test --pass-with-no-tests"}}\n',
71
99
  );
72
100
  await workspace.write('packages/example/tsconfig.lib.json', '{}\n');
73
101
  await workspace.write('packages/example/tsconfig.test.json', '{}\n');
@@ -76,6 +104,7 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
76
104
 
77
105
  expect(targets['tsc-js']?.executor).toBe('@smoothbricks/nx-plugin:typescript-emit');
78
106
  expect(targets['tsc-js']?.options).toEqual({
107
+ executableOutputs: ['./dist/bin/example.js'],
79
108
  tsConfig: 'tsconfig.lib.json',
80
109
  cwd: 'packages/example',
81
110
  });
@@ -305,9 +334,10 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
305
334
  expect(targets['cargo-test-ferris-wasm']?.dependsOn).toEqual(['cargo-fetch', 'cargo-test-ferris-core']);
306
335
  // `--workspace -E 'package(...)'`, never `--package`: the filterset picks
307
336
  // the same tests without re-resolving features, so the run reuses what
308
- // cargo-test-compile built instead of rebuilding inside the bound.
337
+ // cargo-test-compile built instead of rebuilding inside the bound. A
338
+ // workspace member with no tests is still a successful per-crate target.
309
339
  expect(targets['cargo-test-ferris-core']?.options?.command).toMatch(
310
- /^cargo --frozen nextest run --workspace -E 'package\(ferris-core\)' --user-config-file none --config-file /,
340
+ /^cargo --frozen nextest run --workspace -E 'package\(ferris-core\)' --no-tests=pass --user-config-file none --config-file /,
311
341
  );
312
342
  expect(targets['cargo-test-ferris-core']?.inputs).toEqual([
313
343
  '{projectRoot}/Cargo.toml',
@@ -338,6 +368,143 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
338
368
  }
339
369
  });
340
370
 
371
+ it('attributes one repository-root Cargo workspace across package projects', async () => {
372
+ const workspace = await createWorkspace();
373
+ try {
374
+ await workspace.write('package.json', '{"name":"@axe.sc/codebase"}\n');
375
+ await workspace.write('packages/runtime/package.json', '{"name":"@axe.sc/runtime","nx":{"name":"runtime"}}\n');
376
+ await workspace.write('packages/wasm/package.json', '{"name":"@axe.sc/wasm","nx":{"name":"wasm"}}\n');
377
+ await workspace.write(
378
+ 'packages/native/package.json',
379
+ JSON.stringify({
380
+ name: '@axe.sc/native',
381
+ nx: { name: 'native' },
382
+ napi: {
383
+ binaryName: 'native',
384
+ targets: ['aarch64-apple-darwin'],
385
+ },
386
+ }),
387
+ );
388
+ await workspace.write(
389
+ 'Cargo.toml',
390
+ [
391
+ '[workspace]',
392
+ 'members = ["packages/runtime/crates/*", "packages/wasm", "packages/native/crates/*"]',
393
+ '',
394
+ ].join('\n'),
395
+ );
396
+ await workspace.write('Cargo.lock', 'version = 4\n');
397
+ await workspace.write(
398
+ 'packages/runtime/crates/runtime-core/Cargo.toml',
399
+ '[package]\nname = "runtime-core"\n\n[package.metadata.smoothbricks.test]\nshards = 2\n',
400
+ );
401
+ await workspace.write(
402
+ 'packages/wasm/Cargo.toml',
403
+ [
404
+ '[package]',
405
+ 'name = "runtime-wasm"',
406
+ '',
407
+ '[lib]',
408
+ 'crate-type = ["cdylib", "rlib"]',
409
+ '',
410
+ '[package.metadata.smoothbricks.wasm-bindgen]',
411
+ 'targets = ["web"]',
412
+ '',
413
+ ].join('\n'),
414
+ );
415
+ await workspace.write(
416
+ 'packages/native/crates/native-napi/Cargo.toml',
417
+ '[package]\nname = "native-napi"\n\n[lib]\ncrate-type = ["cdylib"]\n',
418
+ );
419
+
420
+ const inferred = await inferTargets(
421
+ ['package.json', 'packages/runtime/package.json', 'packages/wasm/package.json', 'packages/native/package.json'],
422
+ undefined,
423
+ workspace.context,
424
+ );
425
+ const projects = new Map(inferred.flatMap(([, result]) => Object.entries(result.projects ?? {})));
426
+ const root = projects.get('.')?.targets ?? {};
427
+ const runtime = projects.get('packages/runtime')?.targets ?? {};
428
+ const wasm = projects.get('packages/wasm')?.targets ?? {};
429
+ const native = projects.get('packages/native')?.targets ?? {};
430
+ const rootProject = '@axe.sc/codebase';
431
+ const rootFetch = { projects: [rootProject], target: 'cargo-fetch' };
432
+ const rootCompile = { projects: [rootProject], target: 'cargo-test-compile' };
433
+
434
+ expect(
435
+ [...projects]
436
+ .filter(([, project]) => project.targets?.['cargo-test-compile'])
437
+ .map(([projectRoot]) => projectRoot),
438
+ ).toEqual(['.']);
439
+ expect(root['cargo-fetch']?.options).toEqual({
440
+ commands: ['cargo fetch --locked'],
441
+ cwd: '.',
442
+ parallel: false,
443
+ });
444
+ expect(root['cargo-test-compile']?.options).toMatchObject({
445
+ command: 'cargo --frozen test --workspace --no-run',
446
+ cwd: '.',
447
+ });
448
+ expect(root['cargo-lint']?.options).toMatchObject({ cwd: '.' });
449
+ expect(root['cargo-test']?.dependsOn).toHaveLength(5);
450
+
451
+ expect(runtime['cargo-test-runtime-core-shard1']?.dependsOn).toEqual([
452
+ rootFetch,
453
+ { projects: ['native'], target: 'cargo-test-native-napi' },
454
+ ]);
455
+ expect(runtime['cargo-test-runtime-core-shard2']?.dependsOn).toEqual([
456
+ rootFetch,
457
+ 'cargo-test-runtime-core-shard1',
458
+ ]);
459
+ expect(runtime['cargo-test-runtime-core-exceptions']?.dependsOn).toEqual([
460
+ rootFetch,
461
+ 'cargo-test-runtime-core-shard2',
462
+ ]);
463
+ expect(runtime['cargo-test-runtime-core-shard1']?.options).toMatchObject({ cwd: '.' });
464
+ expect(runtime['cargo-test-runtime-core-shard1']?.options?.command).toContain(
465
+ 'nextest run --workspace -p runtime-core',
466
+ );
467
+ expect(native['cargo-test-native-napi']?.options?.command).toContain('--no-tests=pass');
468
+ expect(runtime['cargo-test']?.dependsOn).toEqual([
469
+ 'cargo-test-runtime-core-shard1',
470
+ 'cargo-test-runtime-core-shard2',
471
+ 'cargo-test-runtime-core-exceptions',
472
+ ]);
473
+ expect(runtime['cargo-lint']).toBeUndefined();
474
+ expect(runtime.clean).toBeUndefined();
475
+
476
+ expect(wasm['cargo-test-runtime-wasm']?.dependsOn).toEqual([
477
+ rootFetch,
478
+ { projects: ['runtime'], target: 'cargo-test-runtime-core-exceptions' },
479
+ ]);
480
+ expect(wasm['cargo-wasm']?.options).toMatchObject({ cwd: '.' });
481
+ expect(wasm['cargo-wasm']?.options?.commands).toContain(
482
+ 'cargo --frozen build --release --target wasm32-unknown-unknown --target-dir target/cargo-wasm -p runtime-wasm',
483
+ );
484
+ expect(wasm['cargo-wasm']?.outputs).toEqual(['{projectRoot}/dist-wasm']);
485
+ expect(wasm['cargo-wasm']?.dependsOn).toEqual([rootFetch, '^build']);
486
+ expect(wasm.build?.dependsOn).toContainEqual(rootCompile);
487
+ expect(wasm.clean?.executor).toBe('@smoothbricks/nx-plugin:clean-outputs');
488
+
489
+ expect(native['cargo-test-native-napi']?.dependsOn).toEqual([rootFetch, 'napi-debug']);
490
+ expect(native['napi-debug']?.dependsOn).toContainEqual(rootCompile);
491
+ expect(native['napi-debug']?.options).toMatchObject({
492
+ cwd: '.',
493
+ command:
494
+ 'napi build --platform --no-js --dts native.napi.d.ts --manifest-path packages/native/crates/native-napi/Cargo.toml --package native-napi --output-dir packages/native/.cache/native-debug',
495
+ });
496
+ expect(native['napi-arm64-macos']?.options).toMatchObject({
497
+ cwd: '.',
498
+ command:
499
+ 'napi build --release --platform --no-js --dts native.darwin-arm64.d.ts --target aarch64-apple-darwin --manifest-path packages/native/crates/native-napi/Cargo.toml --package native-napi --output-dir packages/native/dist/native/darwin-arm64',
500
+ });
501
+ expect(native.build?.dependsOn).toContainEqual(rootCompile);
502
+ expect(native.clean?.executor).toBe('@smoothbricks/nx-plugin:clean-outputs');
503
+ } finally {
504
+ await workspace.cleanup();
505
+ }
506
+ });
507
+
341
508
  it('scopes each per-crate cargo-test command to exactly one package', async () => {
342
509
  const workspace = await createWorkspace();
343
510
  try {
@@ -659,7 +826,7 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
659
826
  // not something it inherits from cargo-test-compile's position.
660
827
  expect(targets['cargo-test-cowshed-napi']?.dependsOn).toEqual(['cargo-fetch', 'napi-debug']);
661
828
  expect(targets['cargo-test-cowshed-napi']?.options?.command).toMatch(
662
- /^cargo --frozen nextest run --workspace -E 'package\(cowshed-napi\)' --user-config-file none --config-file /,
829
+ /^cargo --frozen nextest run --workspace -E 'package\(cowshed-napi\)' --no-tests=pass --user-config-file none --config-file /,
663
830
  );
664
831
  expect(targets['napi-test']).toMatchObject({
665
832
  executor: '@smoothbricks/nx-plugin:bounded-exec',
@@ -738,7 +905,7 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
738
905
  // An unsharded crate keeps the bare name and takes no --partition, so
739
906
  // declaring nothing is exactly the old single-target behaviour.
740
907
  expect(targets['cargo-test-small']?.options?.command).toMatch(
741
- /nextest run --workspace -E 'package\(small\)' --user-config-file none/,
908
+ /nextest run --workspace -E 'package\(small\)' --no-tests=pass --user-config-file none/,
742
909
  );
743
910
  // The shards partition the crate MINUS the classes nextest.toml singles
744
911
  // out, i of N. Those are lifted out because a test-group only holds
@@ -751,7 +918,7 @@ describe('@smoothbricks/nx-plugin inferred targets', () => {
751
918
  }
752
919
  for (const index of [1, 2, 3]) {
753
920
  expect(targets[`cargo-test-big-shard${index}`]?.options?.command).toContain(
754
- `--workspace -E 'package(big) and not (${exceptional})' --partition hash:${index}/3`,
921
+ `--workspace -E 'package(big) and not (${exceptional})' --partition hash:${index}/3 --no-tests=pass`,
755
922
  );
756
923
  expect(targets[`cargo-test-big-shard${index}`]?.options?.timeoutMs).toBe(BOUNDED_TEST_TIMEOUT_MS);
757
924
  }