-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MRL enforces tilde not caret for internal deps (#464)
* MRL enforces tilde not caret for internal deps * Cleanup
- Loading branch information
1 parent
343de0a
commit 577cbd6
Showing
4 changed files
with
91 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
import { | ||
alphabeticalDependencies, | ||
alphabeticalScripts, | ||
createRuleFactory, | ||
fileContents, | ||
packageEntry, | ||
packageOrder, | ||
|
@@ -75,6 +76,61 @@ const esmOnlyPackages = [ | |
// "@osdk/examples.*", but they have their own config cause they are nonstandard | ||
]; | ||
|
||
/** | ||
* We don't want to allow `workspace:^` in our dependencies because our current release branch | ||
* strategy only allows for patch changes in the release branch and minors elsewhere. | ||
* | ||
* If we were to allow `workspace:^`, then the follow scenario causes issues: | ||
* - Suppose we have a Foo and a Bar package and Bar depends on Foo. | ||
* - at T0 we cut a release/1.1.x branch and ship [email protected], [email protected] | ||
* - at T1 we cut a release 1.2.x branch and ship [email protected] | ||
* | ||
* If we have `workspace:^` in our deps, a user that already has `[email protected]` in their package.json | ||
* could update their dependencies without updating Bar (say via pnpm update) and Bar's dependency | ||
* on Foo @ `^1.1.0` would be satisfied by the shipped `[email protected]`. | ||
* | ||
* Using `workspace:~` prevents this as `~` can only resolve patch changes. | ||
*/ | ||
const disallowWorkspaceCaret = createRuleFactory({ | ||
name: "disallowWorkspaceCaret", | ||
check: async (context) => { | ||
const packageJson = context.getPackageJson(); | ||
const packageJsonPath = context.getPackageJsonPath(); | ||
|
||
for (const d of ["dependencies", "devDependencies", "peerDependencies"]) { | ||
const deps = packageJson[d] ?? {}; | ||
|
||
for (const [dep, version] of Object.entries(deps)) { | ||
if (version === "workspace:^") { | ||
const message = `'workspace:^' not allowed (${d}['${dep}']).`; | ||
context.addError({ | ||
message, | ||
longMessage: `${message} Did you mean 'workspace:~'?`, | ||
file: context.getPackageJsonPath(), | ||
fixer: () => { | ||
// always refetch in fixer since another fixer may have already changed the file | ||
let packageJson = context.getPackageJson(); | ||
if (packageJson[d]?.[dep] === "workspace:^") { | ||
packageJson[d] = Object.assign( | ||
{}, | ||
packageJson[d], | ||
{ [dep]: "workspace:~" }, | ||
); | ||
|
||
context.host.writeJson( | ||
context.getPackageJsonPath(), | ||
packageJson, | ||
); | ||
} | ||
}, | ||
}); | ||
} | ||
} | ||
} | ||
}, | ||
validateOptions: () => {}, // no options right now | ||
}); | ||
|
||
const cache = new Map(); | ||
|
||
/** | ||
|
@@ -157,6 +213,7 @@ function getTsconfigOptions(baseTsconfigPath, opts) { | |
* skipTsconfigReferences?: boolean | ||
* singlePackageName?: string | ||
* }} options | ||
* @returns {import("@monorepolint/config").RuleModule[]} | ||
*/ | ||
function standardPackageRules(shared, options) { | ||
if (options.esmOnly && options.legacy) { | ||
|
@@ -171,6 +228,8 @@ function standardPackageRules(shared, options) { | |
.slice(0, -1); // drop trailing slash | ||
|
||
return [ | ||
disallowWorkspaceCaret({ ...shared }), | ||
|
||
standardTsconfig({ | ||
...shared, | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.