Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Use resolve instead of Node.JS require for resolving configuration files #1172

Merged
merged 21 commits into from
Apr 29, 2016

Conversation

janslow
Copy link
Contributor

@janslow janslow commented Apr 27, 2016

Fixes #1171 by using resolve to resolve package-based extension configuration files from the directory of the current configuration file.

I've modified the tests for extends so that they have their own node_modules (created by the run:installTestDeps task) in ./test/config, so that tslint-test-config and tslint-test-custom-rules can't be accessed by tslint directly with require().

"Review the Node lookup algorithm (https://nodejs.org/api/modules.html#modules_all_together) " +
"for the approximate method TSLint uses to find the referenced configuration file.");
}
const basedir = relativeTo || process.cwd();
Copy link
Contributor

@jkillian jkillian Apr 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love how you simplified this code!

I'm wondering if we should also attempt to load with regular require.resolve as well. I.e. does it also make sense to look for the node module relative to the installed location of TSLint? I think it does, but only if the "path" specified is not a path but instead a package reference

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would mean it's not the same as the [require.resolve() algorithm](https://nodejs.org/dist/latest-v4.x/docs/api/modules.html#modules_all_together). Before this pull request Y(in the algorithm) was always the location of the tslint package, now it's the location of the parenttslint.json` file (or the tslint package the first time).

It's similar to how you'd expect node -e "require('foo')" to fail if foo was installed globally but not locally.

However, it's arguably a breaking change, so I'm happy to add a fallback which resolves with require.resolve to avoid issues for other users.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with you that ideally extends references in a tslint.json file should only reference packages installed where the file is, but I can see this being a situation where people are relying on other cases, so let's prevent the breaking change here

@jkillian
Copy link
Contributor

I've modified the tests for extends so that they have their own node_modules (created by the run:installTestDeps task)

Clever, seems like a good way to test things out! Seems like it's causing the Windows CI to fail though, any ideas?

image

@janslow
Copy link
Contributor Author

janslow commented Apr 27, 2016

It may be as simple as adding npm as a dev dependency. If not, I'll try using a grunt plugin like grunt-npm-install or grunt-npm-command.

Unfortunately, I can't reproduce the test failure locally, so I'm just guessing at the moment

@jkillian
Copy link
Contributor

It may be as simple as adding npm as a dev dependency. If not, I'll try using a grunt plugin like grunt-npm-install or grunt-npm-command.

Nice fix on this issue, looks like grunt-npm-command solves things well

@janslow
Copy link
Contributor Author

janslow commented Apr 28, 2016

These commits should maintain backwards compatibility with packages installed relative to tslint, with some extra unit tests and CLI tests.

@adidahiya adidahiya added this to the TSLint v3.9 milestone Apr 29, 2016

# check that a tslint file (outside of the resolution path of the tslint package) can resolve a package that is
# installed as a dependency of tslint. See palantir/tslint#1172 for details.
tmpDir=$(mktemp -d)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the version of mktemp installed on my Mac, this is unfortunately an invalid usage. I'm okay with getting rid of this test here though, as you have a similar test below. Plus your test below should work on Windows also, which is great.

@jkillian
Copy link
Contributor

This looks great to me! I'll make the one change I mentioned above in a follow-up commit

@jkillian jkillian merged commit 946ab3c into palantir:master Apr 29, 2016
tomduncalf pushed a commit to tomduncalf/tslint that referenced this pull request Jun 14, 2016
…files (palantir#1172)

* Add `resolve` dependency (and custom typings)

* Use resolve instead of require in resolveConfigurationPaths

Fixes palantir#1171

* Simplify resolveConfigurationPath

`resolve` can handle relative and absolute links.

* path-is-absolute is no longer needed.

* Install test packages in subdirectory, so they aren't in the scope of the main tslint binary

* Run `npm install` in test config directory to install test packages.

* Fix code style issue

* Add dev dependency on npm for grunt run:installTestDeps task

* Revert "Add dev dependency on npm for grunt run:installTestDeps task"

This reverts commit fa0b947.

* Replace run:installTestDeps task with npm-command:test

* Fix lint errors in Gruntfile

* Resolve config file relative to the cwd if it can't be found relative to the parent config

* Make rules in tslint-test-custom-rules package valid rules (copied from noFailRule)

* Add CLI test with a relative extend config

* Add test config package

And install it as a dev dependency

* Add CLI test for extending a package which is installed in tslint.

* Make test packages private

* Fix entry point of test packages

* Simplify non-relative test package

* Add unit test for loadConfigurationFromPath with configs outside of the tslint require path.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

global tslint throws error if extends config option references local package
3 participants