nrwl / nx

Smart Monorepos ยท Fast CI
https://nx.dev
MIT License
23.29k stars 2.32k forks source link

glob works wrong with root not equals to workspace root #27091

Open de-don opened 2 months ago

de-don commented 2 months ago

Current Behavior

When I am trying to use FsTree + glob with some sub directory in my repo, glob works from repository root, not from tree.root.

Expected Behavior

glob patterns should be relative from tree.root

GitHub Repo

No response

Steps to Reproduce

  1. create some files:
    file0.txt
    dir1/file1_0.txt
    dir2/file2_0.txt
    dir2/file2_1.txt
    dir3/file2_1.txt
  2. initialize FsTree with root in dir2:
    const tree = new FsTree('dir2', true);
  3. Run glob:
    const txtFiles = await globAsync(tree, ['**/*.txt']);

Nx Report

Node   : 18.18.2
OS     : darwin-arm64
npm    : 9.8.1

nx                 : 19.5.1
@nx/js             : 19.5.1
@nx/jest           : 19.5.1
@nx/linter         : 19.5.1
@nx/eslint         : 19.5.1
@nx/workspace      : 19.5.1
@nx/angular        : 19.5.1
@nx/cypress        : 19.5.1
@nx/devkit         : 19.5.1
@nx/eslint-plugin  : 19.5.1
@nx/playwright     : 19.5.1
@nx/plugin         : 19.5.1
@nx/storybook      : 19.5.1
@nrwl/tao          : 19.5.1
@nx/web            : 19.5.1
@nx/webpack        : 19.5.1
nx-cloud           : 19.0.0
typescript         : 5.1.6
---------------------------------------
Registered Plugins:
@nx/playwright/plugin
@front/nx-i18n
@front/nx-linters
@front/nx-css-utils
---------------------------------------
Community plugins:
@front/angular-builder : 16.2.0
@storybook/angular     : 7.6.20
nx-stylelint           : 17.1.5
---------------------------------------
Local workspace plugins:
         @front/nx-css-utils
         @front/nx-linters
         @front/nx-i18n
         @mono/generators
         @mono/executors
         @mono/utils

Failure Logs

No response

Package Manager Version

No response

Operating System

Additional Information

Currentry I am fixed this with prefix:

  const txtFiles = await globAsync(tree, [tree.root + '/**/*.txt']);
AgentEnder commented 1 month ago

Hey @de-don ๐Ÿ‘‹

The FsTree is not really meant to be created by code outside of Nx's control. Can you go into a bit more detail about your use case? The glob method uses the underlying Nx workspace context's stored file information to perform the glob. If the root is not the root of the nx workspace this will be inaccurate as you have seen, but remaking a workspace-context for a subdir just to perform a glob would be overkill.

Depending on what your need is my gut would be to recommend creating a glob function in your codebase rather than using the devkit glob, but I'd like some more info before saying that.

de-don commented 1 month ago

@AgentEnder I use FsTree in my executors to create and edit files. Unlike generators, executors do not provide a tree. Using FsTree allows me to:

github-actions[bot] commented 1 month ago

This issue has been automatically marked as stale because more information has not been provided within 7 days. It will be closed in 21 days if no information is provided. If information has been provided, please reply to keep it active. Thanks for being a part of the Nx community! ๐Ÿ™

de-don commented 1 month ago

up :)