Skip to content

Commit

Permalink
fix(extract): ignore entries in 'workspaces' arrays that aren't strin…
Browse files Browse the repository at this point in the history
…gs (instead of throwing) (#955)

## Description

- ignore entries in 'workspaces' arrays that aren't strings, instead of
throwing an exception


Yarn workspaces is an array of strings according to [its
documentation](https://yarnpkg.com/configuration/manifest#workspaces).
Apparently is possible (and accepted?) to have other types than strings
as well. Not sure whether that is _valid_, but at least it's happening
in the field, witnessing #947, so we deal with it.

## Motivation and Context

Addresses the error mentioned in #947 (and likely fixes it)

## How Has This Been Tested?

- [x] green ci
- [x] additional automated non-regression tests

## Types of changes

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] Documentation only change
- [ ] Refactor (non-breaking change which fixes an issue without
changing functionality)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)

## Checklist

- [x] 📖

  - My change doesn't require a documentation update, or ...
  - it _does_ and I have updated it

- [x] ⚖️
- The contribution will be subject to [The MIT
license](https://github.com/sverweij/dependency-cruiser/blob/main/LICENSE),
and I'm OK with that.
  - The contribution is my own original work.
- I am ok with the stuff in
[**CONTRIBUTING.md**](https://github.com/sverweij/dependency-cruiser/blob/main/.github/CONTRIBUTING.md).
  • Loading branch information
sverweij authored Aug 31, 2024
1 parent bb215ae commit e414c0a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/extract/resolve/module-classifiers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,15 @@ function isWorkspaceAliased(pModuleName, pResolvedModuleName, pManifest) {
// oh and: ```picomatch.isMatch('asdf', 'asdf/**') === true``` so
// in case it's only 'asdf' that's in the resolved module name for some reason
// we're good as well.
//
// workspaces is supposed to be array of strings, where each string is
// a glob pattern. However, in the field there's occasions where it's
// not a string. We'll just ignore those for now, hence the presence of
// the `typeof pWorkspace === "string"` check.
const lModuleFriendlyWorkspaceGlobs = lWorkspaces.map((pWorkspace) =>
pWorkspace.endsWith("/") ? `${pWorkspace}**` : `${pWorkspace}/**`,
typeof pWorkspace === "string" && pWorkspace.endsWith("/")
? `${pWorkspace}**`
: `${pWorkspace}/**`,
);
if (picomatch.isMatch(pResolvedModuleName, lModuleFriendlyWorkspaceGlobs)) {
return true;
Expand Down
38 changes: 38 additions & 0 deletions test/extract/resolve/module-classifiers.get-alias-types.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,44 @@ describe("[I] extract/resolve/module-classifiers - getAliasTypes", () => {
);
});

it("doesn't run aliased and aliased-workspace for when workspaces is an array, but the entry isn't a string", () => {
const lManifest = {
name: "test",
version: "1.0.0",
dependencies: {},
workspaces: [{ "packages/*": "packages/*" }],
};
const lResolveOptions = {};
deepEqual(
getAliasTypes(
"some-workspaced-local-package",
"packages/a-package/index.js",
lResolveOptions,
lManifest,
),
[],
);
});

it("skips over entries in the workspaces array that aren't string, but still uses the rest", () => {
const lManifest = {
name: "test",
version: "1.0.0",
dependencies: {},
workspaces: [{ "packages/*": "packages/*" }, "packages/*"],
};
const lResolveOptions = {};
deepEqual(
getAliasTypes(
"some-workspaced-local-package",
"packages/a-package/index.js",
lResolveOptions,
lManifest,
),
["aliased", "aliased-workspace"],
);
});

it("classifies as a webpack alias if it could be both a webpack alias _and_ a workspace alias", () => {
const lManifest = {
name: "test",
Expand Down

0 comments on commit e414c0a

Please sign in to comment.