nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.58k stars 2.36k forks source link

NX: Bindings not found on MacOS when using custom generators #13879

Closed DzmVasileusky closed 1 year ago

DzmVasileusky commented 1 year ago

Current Behavior

We have a custom generator like this

generator.ts

import { libraryGenerator } from '@nrwl/angular/generators';
import { generateFiles, getImportPath, getWorkspaceLayout, names, Tree } from '@nrwl/devkit';
import { Linter } from '@nrwl/linter';
import * as path from 'path';
import { Schema } from './schema';
import addLintingGenerator from '../add-crx-linting/generator';

export async function testLibraryGenerator (tree: Tree, schema: Schema): Promise<void> {

    const name = names(schema.name).fileName;
    const projectDirectory = schema.directory ? `${names(schema.directory).fileName}/${name}` : name;
    const projectName = projectDirectory.replace(new RegExp('/', 'g'), '-').replace(/-\d+/g, '');
    const projectRoot = `${getWorkspaceLayout(tree).libsDir}/${projectDirectory}`;

    const tags = `${schema.tags ? `${schema.tags},` : ''}type:${schema.type}`;

    await libraryGenerator(tree, {
        directory: schema.directory,
        linter: Linter.None,
        name,
        prefix: 'test',
        tags
    });

    const { npmScope } = getWorkspaceLayout(tree);
    const importPath = getImportPath(npmScope, projectDirectory);

    await addLintingGenerator(tree, { importPath, projectName, projectRoot });

    // TODO: convert to generator for use in other generators
    await convertEntryFile(tree, `${projectRoot}/src`);

    return generateFiles(tree, path.join(__dirname, './files'), projectRoot, { ...schema, tpl: '' });

}

async function convertEntryFile (tree: Tree, sourcePath: string) {

    const indexPath = `${sourcePath}/index.ts`;
    const publicApiPath = `${sourcePath}/lib/public_api.ts`;
    const indexContent = tree.read(indexPath)?.toString() ?? '';
    const publicApiContent = indexContent.replace(/\/lib/g, '');

    tree.write(publicApiPath, publicApiContent);
    tree.delete(indexPath);

    return tree;

}

export default testLibraryGenerator;

schema.json

{
  "$schema": "http://json-schema.org/schema",
  "cli": "nx",
  "$id": "Library",
  "title": "Create a Test Library",
  "description": "Modified generator to create an angular library specific for the test web project",
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "description": "the name of the library",
      "$default": {
        "$source": "argv",
        "index": 0
      },
      "x-prompt": "What name would you like to use for the library?",
      "pattern": "^[a-zA-Z].*$"
    },
    "type": {
      "type": "string",
      "description": "Library type.",
      "enum": ["data-access", "feature", "ui", "util"],
      "default": "feature",
      "x-prompt": {
        "message": "What type of library would you like to create?",
        "type": "list",
        "items": [
          {
            "label": "Data Access",
            "value": "data-access"
          },
          {
            "label": "Feature",
            "value": "feature"
          },
          {
            "label": "Ui",
            "value": "ui"
          },
          {
            "label": "Util",
            "value": "util"
          }
        ]
      }
    },
    "tags": {
      "type": "string",
      "description": "Add tags to the project (used for linting)",
      "alias": "t"
    },
    "directory": {
      "type": "string",
      "description": "A directory where the project is placed",
      "alias": "d"
    }
  },
  "required": ["name", "type"]
}

Calling it on MacOS is causing an error

dvasilevsky:web dzmvasilevsky$ npx nx g @project/plug:library fingerprinting --directory=util --type=util --verbose

>  NX  Generating @crexi/plug:library

 >  NX   Bindings not found

Error: Bindings not found
    at Compiler.transformSync (/Users/dzmvasilevsky/Work/TulaCo/crexi/web/node_modules/@swc/core/index.js:250:15)
    at transformSync (/Users/dzmvasilevsky/Work/TulaCo/crexi/web/node_modules/@swc/core/index.js:348:21)
    at transformSync (/Users/dzmvasilevsky/Work/TulaCo/crexi/web/node_modules/@swc-node/core/index.ts:69:26)
    at compile (/Users/dzmvasilevsky/Work/TulaCo/crexi/web/node_modules/@swc-node/register/register.ts:44:40)
    at exts (/Users/dzmvasilevsky/Work/TulaCo/crexi/web/node_modules/@swc-node/register/register.ts:55:38)
    at Module._compile (/Users/dzmvasilevsky/Work/TulaCo/crexi/web/node_modules/pirates/lib/index.js:130:29)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Object.newLoader [as .ts] (/Users/dzmvasilevsky/Work/TulaCo/crexi/web/node_modules/pirates/lib/index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)

Expected Behavior

Generator is working on MacOS

Github Repo

No response

Steps to Reproduce

  1. Create a lib with custom generator code from the description
  2. Run npx nx g @project/plug:library fingerprinting --directory=util --type=util --verbose

Nx Report

Node : 16.15.0
   OS   : darwin x64
   npm  : 8.10.0

   nx : 15.0.13
   @nrwl/angular : 15.0.13
   @nrwl/cypress : 15.0.13
   @nrwl/detox : Not Found
   @nrwl/devkit : 15.0.13
   @nrwl/esbuild : Not Found
   @nrwl/eslint-plugin-nx : 15.0.13
   @nrwl/expo : Not Found
   @nrwl/express : Not Found
   @nrwl/jest : 15.0.13
   @nrwl/js : 15.0.13
   @nrwl/linter : 15.0.13
   @nrwl/nest : Not Found
   @nrwl/next : Not Found
   @nrwl/node : Not Found
   @nrwl/nx-cloud : 15.0.2
   @nrwl/nx-plugin : 15.0.13
   @nrwl/react : Not Found
   @nrwl/react-native : Not Found
   @nrwl/rollup : Not Found
   @nrwl/schematics : Not Found
   @nrwl/storybook : 15.0.13
   @nrwl/web : Not Found
   @nrwl/webpack : 15.0.13
   @nrwl/workspace : 15.0.13
   typescript : 4.8.4
   ---------------------------------------
   Local workspace plugins:
     @crexi/plug
   ---------------------------------------
   Community plugins:
     @ngrx/component-store: 14.0.2
     @ngrx/effects: 14.0.2
     @ngrx/entity: 14.0.2
     @ngrx/router-store: 14.0.2
     @ngrx/store: 14.0.2
     @ngrx/component-store: 14.0.2
     @ngrx/effects: 14.0.2
     @ngrx/entity: 14.0.2
     @ngrx/router-store: 14.0.2
     @ngrx/schematics: 14.0.2
     @ngrx/store: 14.0.2
     @ngrx/store-devtools: 14.0.2
     @nguniversal/builders: 14.2.2
     @nguniversal/express-engine: 14.2.2
     @storybook/angular: 6.5.13
     ng-mocks: 14.3.3

Failure Logs

No response

Additional Information

No response

DzmVasileusky commented 1 year ago
    transformSync(src, options) {
        var _a, _b, _c;
        const isModule = typeof src !== "string";
        options = options || {};
        if ((_a = options === null || options === void 0 ? void 0 : options.jsc) === null || _a === void 0 ? void 0 : _a.parser) {
            options.jsc.parser.syntax = (_b = options.jsc.parser.syntax) !== null && _b !== void 0 ? _b : 'ecmascript';
        }
        const { plugin } = options, newOptions = __rest(options, ["plugin"]);
        if (bindings) {
            if (plugin) {
                const m = typeof src === "string" ? this.parseSync(src, (_c = options === null || options === void 0 ? void 0 : options.jsc) === null || _c === void 0 ? void 0 : _c.parser, options.filename) : src;
                return this.transformSync(plugin(m), newOptions);
            }
            return bindings.transformSync(isModule ? JSON.stringify(src) : src, isModule, toBuffer(newOptions));
        }
        else if (fallbackBindings) {
            if (plugin && !this.fallbackBindingsPluginWarningDisplayed) {
                console.warn(`Fallback bindings does not support legacy plugins, it'll be ignored.`);
                this.fallbackBindingsPluginWarningDisplayed = true;
            }
            return fallbackBindings.transformSync(isModule ? JSON.stringify(src) : src, options);
        }
        console.log('11');
        throw new Error("Bindings not found");
    }

this function is failing

DzmVasileusky commented 1 year ago

Solved it here

https://github.com/swc-project/swc/issues/5616

github-actions[bot] commented 1 year ago

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.