-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Conversation
__tests__/index.js
Outdated
@@ -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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!
__tests__/integration.js
Outdated
const cwd = await makeTemp(); | ||
const cacheFolder = path.join(cwd, '.cache'); | ||
|
||
const subdir = path.join(cwd, 'a/b/c/d/e/f/g/h/i'); |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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;
* 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
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 tofoo
iffoo/package.json
exists but notfoo/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.