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

--version and --help might be processing global data files #1313

Closed
cfjedimaster opened this issue Jul 11, 2020 · 16 comments
Closed

--version and --help might be processing global data files #1313

cfjedimaster opened this issue Jul 11, 2020 · 16 comments
Assignees
Labels

Comments

@cfjedimaster
Copy link

Describe the bug
I noticed that eleventy --help was running real slow. I then noticed an error was thrown before the help text. The error was from one of my _data files, and was a valid error, but why would --help, and --version try to run code in my site?

To Reproduce
Output the help or version for the CLI in a directory that has an Eleventy site, possibly with an error in it.

Environment:

  • OS and Version: Windows 10
  • Eleventy Version : 0.11
@cfjedimaster
Copy link
Author

While it seems to be executing code in my site, it isn't generating a _site folder.

@pdehaan
Copy link
Contributor

pdehaan commented Jul 11, 2020

Fascinating!
Looks like stuff might happen here:

eleventy/cmd.js

Lines 48 to 80 in 727607c

let elev = new Eleventy(argv.input, argv.output);
elev.setConfigPathOverride(argv.config);
elev.setPathPrefix(argv.pathprefix);
elev.setDryRun(argv.dryrun);
elev.setIncrementalBuild(argv.incremental);
elev.setPassthroughAll(argv.passthroughall);
elev.setFormats(argv.formats);
// --quiet and --quiet=true resolves to true
if (argv.quiet === true || argv.quiet === false) {
elev.setIsVerbose(!argv.quiet);
}
// careful, we can’t use async/await here to error properly
// with old node versions in `please-upgrade-node` above.
elev
.init()
.then(function() {
if (argv.version) {
console.log(elev.getVersion());
} else if (argv.help) {
console.log(elev.getHelp());
} else if (argv.serve) {
elev.watch().then(function() {
elev.serve(argv.port);
});
} else if (argv.watch) {
elev.watch();
} else {
elev.write();
}
})
.catch(EleventyErrorHandler.fatal);

I wonder if explicitly checking for --version and --help and aborting early would speed things up. I'd have to see if I can figure out how to enable all the debugging output and try testing locally to see if the expensive part is the elev.init() call on L64 which is possibly unneeded for reporting a version or help string (or maybe not, what do I know).

@cfjedimaster
Copy link
Author

I should add, I happened to be in a huge Eleventy project. If I didn't have the error in the code and it was a smaller project, I probably wouldn't have noticed.

@zachleat zachleat added bug and removed needs-triage labels Jun 15, 2021
@zachleat zachleat changed the title Help and version command also run a build --version and --help might be processing global data files Jun 15, 2021
@zachleat zachleat added this to the Eleventy 1.0.0 milestone Jun 15, 2021
@zachleat zachleat self-assigned this Jun 15, 2021
@zachleat
Copy link
Member

Shipping with 1.0

@cfjedimaster
Copy link
Author

I'm using 1.0 beta4 and I still have this issue. In my case, I absolutely have a bug in current site and I want to debug, but I forgot how to do that. I tried eleventy --help to remind me, and all I get is the error for the current site. When you say, "Shipping with 1.0", does that it mean it should work now?

@pdehaan
Copy link
Contributor

pdehaan commented Nov 6, 2021

@cfjedimaster Interesting!

If I build a fake site locally with this global data file:

module.exports = () => {
  console.log(`I'm a global data file: ${__filename}`);
  return {
    foo: "bar",
    baz: false
  };
};

... and this .eleventy.js config file:

module.exports = eleventyComputed => {
  console.log(`I'm a config file: ${__filename}`);

  return {
    dir: {
      output: "www"
    }
  };
};

... then I get the following output:

npm run build
> eleventy

I'm a config file: /private/tmp/11ty-global-help/.eleventy.js
I'm a global data file: /private/tmp/11ty-global-help/_data/site.js
[11ty] Wrote 0 files in 0.02 seconds (v1.0.0-beta.4)

So it processed my config file and global data file like I expect.

If I run eleventy --version, it runs my config file, but I don't see the debug output from my global data file.

eleventy --version

I'm a config file: /private/tmp/11ty-global-help/.eleventy.js
1.0.0-beta.4

And same w/ eleventy --help (processes .eleventy.js config file, but not global data file):

> eleventy --help

I'm a config file: /private/tmp/11ty-global-help/.eleventy.js
Usage: eleventy
       eleventy --input=. --output=./_site
       eleventy --serve

Arguments:

     --version

     --input=.
       Input template files (default: `.`)

     --output=_site
       Write HTML output to this folder (default: `_site`)

     --serve
       Run web server on --port (default 8080) and watch them too

     --watch
       Wait for files to change and automatically rewrite (no web server)

     --formats=liquid,md
       Whitelist only certain template types (default: `*`)

     --quiet
       Don’t print all written files (off by default)

     --config=filename.js
       Override the eleventy config file path (default: `.eleventy.js`)

     --pathprefix='/'
       Change all url template filters to use this subdirectory.

     --dryrun
       Don’t write any files. Useful with `DEBUG=Eleventy* npx eleventy`

     --to=json
     --to=ndjson
       Change the output to JSON or NDJSON (default: `fs`)

     --help

So if eleventy [--help|--version] is still executing the .eleventy.js code, I wonder if you're trying to access any global data files or something from the config file and that's what's causing something.


UPDATE: Garbage repo uploaded to https://github.com/pdehaan/11ty-global-help

@cfjedimaster
Copy link
Author

In my demo code I was using a plugin and the plugin was throwing an error - so another config file.

@pdehaan
Copy link
Contributor

pdehaan commented Nov 7, 2021

Ah yes... I wrote a terrible plugin that throws on command and can confirm that you seemingly can't run eleventy --version or eleventy --help, which feels suboptimal.

// ./plugins/bad-plugin.js
module.exports = (eleventyConfig = {}, pluginConfig = {}) => {
  console.log(`I'm a custom plugin: ${__filename}`);
  if (!!pluginConfig.throws) {
    throw new Error(`Thrown error from a custom plugin`);
  };
  eleventyConfig.addFilter("customFilter", (value) => `plugin: ${value}`);
};
// .eleventy.js
module.exports = eleventyConfig => {
  console.log(`I'm a config file: ${__filename}`);

  eleventyConfig.addPlugin(require("./plugins/bad-plugin"), {throws: true});

  return {
    dir: {
      output: "www"
    }
  };
};

OUTPUT

> eleventy --version

I'm a config file: /private/tmp/11ty-global-help/.eleventy.js
I'm a custom plugin: /private/tmp/11ty-global-help/plugins/bad-plugin.js
[11ty] Eleventy CLI Fatal Error: (more in DEBUG output)
[11ty] > Thrown error from a custom plugin

`Error` was thrown:
[11ty]     Error: Thrown error from a custom plugin
        at module.exports (/private/tmp/11ty-global-help/plugins/bad-plugin.js:4:11)
        at UserConfig._executePlugin (/private/tmp/11ty-global-help/node_modules/@11ty/eleventy/src/UserConfig.js:335:7)
        at /private/tmp/11ty-global-help/node_modules/@11ty/eleventy/src/TemplateConfig.js:187:23
        at Array.forEach (<anonymous>)
        at TemplateConfig.processPlugins (/private/tmp/11ty-global-help/node_modules/@11ty/eleventy/src/TemplateConfig.js:185:29)
        at TemplateConfig.mergeConfig (/private/tmp/11ty-global-help/node_modules/@11ty/eleventy/src/TemplateConfig.js:253:10)
        at TemplateConfig.getConfig (/private/tmp/11ty-global-help/node_modules/@11ty/eleventy/src/TemplateConfig.js:117:26)
        at new Eleventy (/private/tmp/11ty-global-help/node_modules/@11ty/eleventy/src/Eleventy.js:74:39)
        at Object.<anonymous> (/private/tmp/11ty-global-help/node_modules/@11ty/eleventy/cmd.js:70:14)
        at Module._compile (internal/modules/cjs/loader.js:1072:14)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! 11ty-global-help@1.0.0 test:version: `eleventy --version`
npm ERR! Exit status 1

@pdehaan
Copy link
Contributor

pdehaan commented Nov 7, 2021

I think I fixed this locally (by hacking files in my node_modules/ directly), but not sure it's great solution…

If we change the getVersion() and getHelp() methods to static methods...

  /**
   * Reads the version of Eleventy...
   */
  static getVersion() {
    return pkg.version;
  }

  /**
   * Shows a help message including usage...
   */
  static getHelp() {
    return `Usage: eleventy
      ...
  }

... then check argv.version and argv.help arguments and exit early before creating a new Eleventy instance:

  if (argv.version) {
    console.log(Eleventy.getVersion());
    return;
  }
  if (argv.help) {
    console.log(Eleventy.getHelp());
    return;
  }

  let elev = new Eleventy(argv.input, argv.output, {

@zachleat
Copy link
Member

Am I correct in reading this that it was partially fixed but there still exists an issue when a plugin throws an error?

@zachleat
Copy link
Member

Moving this to 1.0.1 milestone

@zachleat zachleat reopened this Jan 10, 2022
@cfjedimaster
Copy link
Author

@zachleat yep

@zachleat
Copy link
Member

Shipping with 1.0.1 and 2.0.0-canary.5

@zachleat
Copy link
Member

(Absolutely used the approach recommended by @pdehaan here #1313 (comment) though kept the instance methods for backwards compat)

zachleat added a commit that referenced this issue Apr 15, 2022
@cfjedimaster
Copy link
Author

Hey sorry, this is back. Eleventy was running super slow because it was processing my node_modules. It took quite some time to run and ended up throwing an error and in one of the eleventy-plugin-webc files.

As soon as I .gitignore node_modules, it works.

@cfjedimaster
Copy link
Author

Sorry disregard - my global eleventy CLI had fallen behind.

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

No branches or pull requests

3 participants