Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better active directory selection #4246

Merged
merged 9 commits into from
Aug 24, 2017
Merged

Conversation

arcanis
Copy link
Member

@arcanis arcanis commented Aug 24, 2017

Summary

Fix #2709 - Yarn will now try to move to the closest ancestor directory that contains a package.json file if it can. If none is to be found, the current directory will be used as usual. Note that the same behavior if applied to the --cwd option, so --cwd foo/bar will go to foo if foo/package.json exists but not foo/bar/package.json.

I thought about doing this in the same pass than the rc file selection, but it wouldn't change anything because we're not stat'ing the same files, so we could not factorize the checks.

Test plan

Added a test case, and the old one should still pass as expected.

@arcanis arcanis changed the title Active directory Better active directory selection Aug 24, 2017
@arcanis arcanis requested a review from BYK August 24, 2017 11:34
@@ -243,7 +243,7 @@ if (process.platform !== 'win32') {
}

test.concurrent('should run bin command', async () => {
const stdout = await execCommand('bin', [], '', false);
const stdout = await execCommand('bin', [], '', true);
Copy link
Member Author

Choose a reason for hiding this comment

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

If not run in a temp directory, the new behavior makes us go to the repository root. Other tests don't seem to break.

Copy link
Member

@BYK BYK left a comment

Choose a reason for hiding this comment

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

Awesome!

const cwd = await makeTemp();
const cacheFolder = path.join(cwd, '.cache');

const subdir = path.join(cwd, 'a/b/c/d/e/f/g/h/i');
Copy link
Member

Choose a reason for hiding this comment

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

Since you're using path.join I'd recommend writing this as path.join(cwd, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i') for cross-platform compatibility. You can even do path.join(cwd, ...Array.from('abcdefghi')) if you wanna look cool :P

src/cli/index.js Outdated
@@ -21,6 +21,22 @@ const net = require('net');
const onDeath = require('death');
const path = require('path');

function findActiveDirectory(base): string {
Copy link
Member

Choose a reason for hiding this comment

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

I'd rename this to findProjectRoot

src/cli/index.js Outdated
let prev = null;
let dir = base;

do {
Copy link
Member

Choose a reason for hiding this comment

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

Can be simplified as:

while (prevDir !== currentDir && !fs.existsSync(path.join(currentDir, constants.NODE_PACKAGE_JSON))) {
    prevDir = currentDir;
    currentDir= path.dirname(currentDir);
}

return currentDir;

@arcanis arcanis merged commit ba96dc4 into yarnpkg:master Aug 24, 2017
joaolucasl pushed a commit to joaolucasl/yarn that referenced this pull request Oct 27, 2017
* Automatically backtrack to the closest package.json directory

* Add a test

* Prettier

* Fixes linting

* Executes a test in a temp directory to not get affected by the change

* Removes now useless await

* Feedbacks

* Update index.js

* Update index.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

yarn ignores existing project package.json
2 participants