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

Add initial cwd to directories for searching config files #154

Closed

Conversation

sttk
Copy link
Contributor

@sttk sttk commented Jan 27, 2018

I've add initCwd entry to the last of opts.configFiles.

This PR is for the issue #146 and changes gulp's behaviors about using config file and selecting gulpfile.

  • There is an ancestor directory having gulpfile.*.
  • And current directory has a config file which has flags.gulpfile property.
  • And current directory has no gulpfile or a gulpfile of which name is not 'gulpfile.*'.

In the situation above, gulp after this modification uses the config file in current dir and uses gulpfile specified in the config file, but changes cwd to the ancestor dir.
Before this modification, gulp ignores this config file, and finds up a gulpfile in the ancestor dir, changes cwd to the dir and uses the gulpfile.

@phated
Copy link
Member

phated commented Mar 13, 2019

@sttk I assume this is going to be changed with the new liftoff stuff?

@sttk
Copy link
Contributor Author

sttk commented Mar 14, 2019

@phated Yes. I think so, too. In addition, I think we can include a change for flags.nodeFlags too with the new liftoff stuff.

@phated
Copy link
Member

phated commented Mar 14, 2019

I think we can include a change for flags.nodeFlags too with the new liftoff stuff.

In separate PRs 😛

@sttk sttk force-pushed the add_initcwd_to_dirs_for_searching_config_files branch from 07fb7c0 to be9a912 Compare March 21, 2019 09:51
@sttk sttk closed this Mar 21, 2019
@sttk sttk force-pushed the add_initcwd_to_dirs_for_searching_config_files branch from dab1404 to a4236f2 Compare March 21, 2019 11:15
@sttk
Copy link
Contributor Author

sttk commented Mar 21, 2019

Sorry. I made a mistake on pushing.

@sttk sttk reopened this Mar 21, 2019
Copy link
Member

@phated phated left a comment

Choose a reason for hiding this comment

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

@sttk Great work, I love how this PR got cleaned up with the Liftoff changes! I just had one question for you.

@@ -89,7 +93,8 @@ function run() {
require: opts.require,
completion: opts.completion,
}, function(env) {
var cfgLoadOrder = ['home', 'cwd'];

var cfgLoadOrder = ['home', 'cwd', 'initCwd'];
Copy link
Member

Choose a reason for hiding this comment

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

@sttk I might be wrong but shouldn't the order be ['home', 'initCwd', 'cwd']? I think that initCwd is a fallback if we can't find one inside cwd, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@phated Perhaps you are right, but one test case fails by this because of the bug that flags.gulpfile does not change cwd. We should fix the bug before this.

@sttk sttk force-pushed the add_initcwd_to_dirs_for_searching_config_files branch from feb590d to 4d8248d Compare March 23, 2019 11:01
stdout = skipLines(eraseTime(stdout), 1);

expect(headLines(stdout, 1)).toEqual(headLines(expected0, 1));
expect(skipLines(stdout, 1)).toEqual(skipLines(expected3, 1));
Copy link
Member

Choose a reason for hiding this comment

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

The behavior of this seems extremely awkward. When using --cwd, I wouldn't expect the flags.gulpfile to be picking up a gulpfile where I ran my command - I'd expect it to run a gulpfile where my --cwd was pointing.

'\n\t--gulpfile', function(done) {
// This test case fails because of a bug that `flags.gullpfile` does not
// change process.cwd().
it.skip('Should use config file here and use gulpfile specified in config file', function(done) {
Copy link
Member

Choose a reason for hiding this comment

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

@sttk I just went through this and I think it's too big of issue to ship these changes without having this working correctly.

I think it's a fundamental flaw in how we added config files to Liftoff, because they don't affect it's configBase, etc lookups when we rename the gulpfile.

I'm not sure how to solve this.

@phated
Copy link
Member

phated commented Mar 23, 2019

@sttk I'm continuing to dig into this and I think the behavior is even more incorrect when initCwd isn't the highest priority. If I run cd my-sub-project; gulp and there's a renamed gulpfile there, my working directory should not be changed to a top-level directory containing a gulpfile.js - as most of these tests indicate.

I'm experimenting with initCwd being the highest priority and also setting the env.cwd if a gulpfile is found in that config.

@phated
Copy link
Member

phated commented Mar 23, 2019

@sttk I've also found that when using a .gulp.* file to reference a gulpfile in another location, the .gulp.* file relative to that gulpfile isn't resolved. I'm about to push some changes and skipped/failing tests that we need to fix before I think this can land. I'm also not sure how we can fix it without deeper integrations in Liftoff with the config files. I'm open to any suggestions.

@phated phated force-pushed the add_initcwd_to_dirs_for_searching_config_files branch from 4d8248d to de48b19 Compare March 23, 2019 23:21
@phated
Copy link
Member

phated commented Mar 23, 2019

@sttk I've just force-pushed a rebase on the lastest master plus my changes where I think our behavior is incorrect. I've tried to note in comments how things aren't working correctly or the temporary solutions to some problems exist. Please let me know what you think of all of this.

@sttk
Copy link
Contributor Author

sttk commented Mar 24, 2019

@phated This issue is very difficult. Please give me some time to think.

@sttk
Copy link
Contributor Author

sttk commented Mar 25, 2019

@phated Does what your changes suggest that if a config file in initCwd has flags.gulpfile or flags.cwd, gulp-cli should change cwd to env.cwd and then loads .gulp.* file in the changed env.cwd?

It means that flags by config files in initCwd behaves like flags by CLI options?

@sttk
Copy link
Contributor Author

sttk commented Mar 25, 2019

If so, I have two ideas to fix this issue: (The following codes are not actual but an example.)

  1. Process a config file in initCwd before cli.prepare.
var cli = new Liftoff({ ... });
var foundInInitCwd = fined({ path: '.', name: '.gulp', extensions: cli.extensions });
var cfg = { flags: {} };
if (foundInInitCwd) {
  cfg = require(foundInInitCwd.path);
  opts = mergeConfigToCliFlags(opts, cfg);
  if (!opts.gulpfile) { opts.gulpfile = cfg.flags.gulpfile; }
  if (!opts.cwd) { opts.cwd = cfg.flags.cwd || cfg.flags.gulpfile; }
}

cli.prepare({ cwd: opts.cwd, configPath: opts.gulpfile, ... }, function(env) {
  var cfgLoadOrder = ['home'];
  if (env.configFiles['.gulp']['cwd'].path !== foundInInitCwd.path) { cfgLoadOrder.push('cwd'); }
  var cfg = loadConfigFiles(env.configFiles['.gulp'], cfgLoadOrder);
  opts = mergeConfigToCliFlags(opts, cfg);
  env = mergeConfigToEnvFlags(env, cfg, opts);
   ...
  cli.execute(env, handleArguments);
});
  1. Apply the PR #95
var opts = { ... };
var env = {};
var cli = new Liftoff({
    ...
    configFiles: {
      '.gulp': {
        initCwd: {
          order: 1,
          path: opts.cwd || process.env.INIT_CWD,
          onFound: loadAndMergeConfig,},
        cwd: { 
         order: 2,
          path: env.cwd
          onFound: loadAndMergeConfig,},
        home: {
          order: 3,
          path: '~',
          onFound: loadAndMergeConfig,},
      },
    },
});
cli.prepare({ cwd: opts.cwd, configPath: opts.gulpfile, ... }, function(env) {
   ...
  cli.execute(env, handleArguments);
});

function loadAndMergeConfigs(name, path) {
  var cfg = require(path);
  opts = mergeConfigToCliFlags(opts, cfg);
  env = mergeConfigToEnvFlags(env, cfg, opts);
}

@phated
Copy link
Member

phated commented Mar 26, 2019

@sttk I think the issue is actually that too much filesystem traversal is being made in Liftoff.prototype.buildEnvironment - I'm wondering if we shouldn't move https://github.com/js-cli/js-liftoff/blob/master/index.js#L62-L111 into the execute phase.

The reason I think this is that the cwd config file entry in the following code should actually be searching the process.cwd() (or --cwd or the resolved dirname of --gulpfile if either are provided)

gulp-cli/index.js

Lines 43 to 54 in 1b80d67

configFiles: {
'.gulp': {
home: {
path: '~',
extensions: interpret.extensions,
},
cwd: {
path: '.',
extensions: interpret.extensions,
},
},
},

I believe that is what you were trying to achieve by adding initCwd config file entry. If config files were always started at the cwd the same way that gulpfiles are looked up, before we attempt to look up the gulpfile, I believe the issue would be solved. Though, there might be an issue with a .gulp.* file existing at a different place in the tree, but traversing up the filesystem should handle that, I think.

@sttk
Copy link
Contributor Author

sttk commented Mar 27, 2019

I'm wondering if we shouldn't move https://github.com/js-cli/js-liftoff/blob/master/index.js#L62-L111 into the execute phase.

Rather, that moving may be necessary because there is a case that gulp cannot be run only by changing cwd and gulpfile. It's needed to set env.modulePath and env.modulePackage too by executing those part in the link.

$ find . -path './b/node_modules/*' -prune -o -print
.
./a
./a/.gulp.json    => `{ "flags.gulpfile": "../b/gulpfile.js" }`
./a/gulpfile.js    // This is a dummy gulpfile to make gulp-cli load `a/.gulp.json`
./b
./b/gulpfile.js
./b/node_modules
./b/package.json

$ cd a
$ gulp --gulpfile ../b/gulpfile.js
[00:23:11] Working directory changed to ~/path/to/b
[00:23:11] Using gulpfile ~/path/to/b/gulpfile.js
[00:23:11] Starting 'default'...
[00:23:11] Finished 'default' after 2.71 ms

$ gulp
[00:24:57] Local gulp not found in ~/path/to/a
[00:24:57] Try running: npm install gulp

@sttk
Copy link
Contributor Author

sttk commented Mar 27, 2019

Furthermore, the current gulp with --cwd option loads and processes .gulp.* in the chaged directory.
I think this behavior is correct, but to reproduce the same behavior with a config file it is needed to load and process .gulp.* in initCwd before resolving paths of other config files.

$ find . -path './b/node_modules/*' -prune -o -print
.
./a
./a/.gulp.json    => `{ "flags.gulpfile": "../b/gulpfile.js" }`
./a/gulpfile.js
./b
./b/.gulp.json    => `{ "flags.require": "ansi-colors" }`
./b/gulpfile.js
./b/node_modules
./b/package.json

$ cd a
$ gulp --cwd ../b
[23:38:20] Requiring external module ansi-colors     // => Load and process b/.gulp.json
[23:38:20] Working directory changed to ~/path/to/b
[23:38:20] Using gulpfile ~/path/to/b/gulpfile.js
[23:38:20] Starting 'default'...
[23:38:20] Finished 'default' after 2.4 ms

@phated
Copy link
Member

phated commented Jan 7, 2024

@sttk I believe this needs conflicts resolved since I merged #239

@phated phated closed this in #253 Jan 25, 2024
@phated phated moved this from In progress to Done in v5 Jan 25, 2024
@phated phated moved this from In Progress to Done in v4 Mar 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
v4
  
Done
v5
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

2 participants