Skip to content

[flutter_tools] Discover pubspec.yaml in parent directories#48548

Merged
fluttergithubbot merged 5 commits intoflutter:masterfrom
jonahwilliams:walk_up_fs
Jan 28, 2020
Merged

[flutter_tools] Discover pubspec.yaml in parent directories#48548
fluttergithubbot merged 5 commits intoflutter:masterfrom
jonahwilliams:walk_up_fs

Conversation

@jonahwilliams
Copy link
Copy Markdown
Contributor

If there is no pubspec.yaml in the current directory, walk upwards until we either find one or run out of parents.

Fixes #48117

@fluttergithubbot fluttergithubbot added the tool Affects the "flutter" command-line tool. See also t: labels. label Jan 10, 2020
Copy link
Copy Markdown
Contributor

@dnfield dnfield left a comment

Choose a reason for hiding this comment

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

I really like this idea, and I think that 99% of the time it would work just right.

I'm a bit worried about the 1% of the time where I have a random nested Flutter project and now don't know which pubspec I'm getting where I used to get an error that I was in the wrong directory. Or if I (accidentally?) copy a pubspec.yaml into my $HOME/src/ directory and all of the sudden the tool treats any folder under that as a flutter project.

@jonahwilliams jonahwilliams changed the title [flutter_tools] Discovery pubspec.yaml in parent directories [flutter_tools] Discover pubspec.yaml in parent directories Jan 10, 2020
@jonahwilliams
Copy link
Copy Markdown
Contributor Author

I thought about that as well, but note that a failing flutter run would never irrecoverably alter the environment. I think the worst thing that could happen is an unclear error message.

while (!globals.fs.isFileSync('pubspec.yaml')) {
final Directory nextCurrent = globals.fs.currentDirectory.parent;
if (nextCurrent == null || nextCurrent.path == globals.fs.currentDirectory.path) {
throw ToolExit(userMessages.flutterNoPubspec);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe we could add something in here about which directories we traversed and failed to find?

if (nextCurrent == null || nextCurrent.path == globals.fs.currentDirectory.path) {
throw ToolExit(userMessages.flutterNoPubspec);
}
globals.fs.currentDirectory = nextCurrent;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If this loop succeeds, it would probably help if we printed a status saying what directory we are using as the cwd.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Only if we are not using the cwd we started with

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The action would be to change your cwd if something has gone wrong, right?

Copy link
Copy Markdown
Contributor

@dnfield dnfield Jan 10, 2020

Choose a reason for hiding this comment

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

Imagine, for example, the following directory structures:

src/
  pubspec.yaml           // put here last week when I didn't trust git to not lose it on me
  flutter_app_1/
    pubspec.yaml
    android/
  java_app_1/
    // no pubspec.yaml
  lib/
    main.dart

One day I do flutter run in java_app_1 and to my surprise weird things happen. I would have found it really helpful to know that it was because the tool found a stray pubspec.yaml that I left laying around on my FS.

Alternatively, imagine I did flutter run -t ../lib/main.dart from the android directory. And imagine that, for some crazy reason, I have a ../../lib/main.dart present in my filesystem. The log message would have made it clearer which main.dart was actually getting used.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Won't the dart process at least potentially be resolving relative paths, such as the -t option to flutter run?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure, but those might be wrong anyway. At any rate, we're still strictly better than we were before:

  1. flutter run (in lib/): failed
  2. flutter run --target=main.dart (in lib/): failed

After:

  1. flutter run (in lib/): passed
  2. flutter run --target=main.dart (in lib/): failed

If this leads to confusion, we can always tweak the error messaging

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What's the output of flutter run --target=main.dart in lib/ with this patch?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Target file "main.dart" not found.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Okay, I'll back out my comment on non-actionable message :) . Like you suggested, we status log to the new CWD if it is updated, then the error message above is more interpretable

fail('Expect exception');
} catch (e) {
expect(e, isInstanceOf<ToolExit>());
expect(e.toString(), contains('Target file "lib/main.dart" not found.'));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should we also expect to find the log told us what directory we ended up in? Or am I misunderstanding what this test is actually setting up?

@jonahwilliams jonahwilliams requested a review from dnfield January 28, 2020 01:42
Copy link
Copy Markdown
Contributor

@dnfield dnfield left a comment

Choose a reason for hiding this comment

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

Lgtm

@fluttergithubbot fluttergithubbot merged commit c341d4b into flutter:master Jan 28, 2020
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Aug 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When looking for a pubpec.yaml, tool should also look up the filesystem

5 participants