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

Issue running grep with input globs using script interface #555

Closed
navin-moorthy opened this issue Apr 5, 2023 · 9 comments
Closed

Issue running grep with input globs using script interface #555

navin-moorthy opened this issue Apr 5, 2023 · 9 comments

Comments

@navin-moorthy
Copy link

Error in terminal: grep: **/*.*: no such file or directory

const { stdout } = await $({
	shell: true,
})`grep --exclude script/setup.js --exclude script/setup-test-e2e.js --exclude-dir node_modules -i ${search} *.* **/*.*`;

References: JoshuaKGoldberg/create-typescript-app#357, JoshuaKGoldberg/create-typescript-app#349 (comment)

@ehmicky
Copy link
Collaborator

ehmicky commented Apr 5, 2023

Hi @navin-moorthy,

Which error do you get when running the following command?

import { execFile } from 'child_process'
import { promisify } from 'util'

const pExecFile = promisify(execFile)

const { stdout } = await pExecFile(`grep --exclude script/setup.js --exclude script/setup-test-e2e.js --exclude-dir node_modules -i *.* **/*.*`, { shell: true })

@navin-moorthy
Copy link
Author

navin-moorthy commented Apr 6, 2023

I do not get any error while running pExecFile with { shell: true }.

But I get this error while running without { shell: true },

Error Details

> node script/setup-test-e2e.js

node:internal/errors:490
    ErrorCaptureStackTrace(err);
    ^

Error: spawn grep --exclude script/setup.js --exclude script/setup-test-e2e.js --exclude-dir node_modules -i /JoshuaKGoldberg/ *.* **/*.* ENOENT
    at ChildProcess._handle.onexit (node:internal/child_process:283:19)
    at onErrorNT (node:internal/child_process:476:16)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'spawn grep --exclude script/setup.js --exclude script/setup-test-e2e.js --exclude-dir node_modules -i /JoshuaKGoldberg/ *.* **/*.*',
  path: 'grep --exclude script/setup.js --exclude script/setup-test-e2e.js --exclude-dir node_modules -i /JoshuaKGoldberg/ *.* **/*.*',
  spawnargs: [],
  cmd: 'grep --exclude script/setup.js --exclude script/setup-test-e2e.js --exclude-dir node_modules -i /JoshuaKGoldberg/ *.* **/*.*',
  stdout: '',
  stderr: ''
}

While running below command,

const { stdout } = await $`grep --exclude script/setup.js --exclude script/setup-test-e2e.js --exclude-dir node_modules -i /JoshuaKGoldberg/ *.* **/*.*`;

I get this error,

Error Details

> node script/setup-test-e2e.js

file:///Users/github/template-typescript-node-package/node_modules/.pnpm/execa@7.1.1/node_modules/execa/lib/error.js:59
		error = new Error(message);
		        ^

Error: Command failed with exit code 2: grep --exclude script/setup.js --exclude script/setup-test-e2e.js --exclude-dir node_modules -i /JoshuaKGoldberg/ *.* **/*.*
grep: *.*: No such file or directory
grep: **/*.*: No such file or directory
    at makeError (file:///Users/github/template-typescript-node-package/node_modules/.pnpm/execa@7.1.1/node_modules/execa/lib/error.js:59:11)
    at handlePromise (file:///Users/github/template-typescript-node-package/node_modules/.pnpm/execa@7.1.1/node_modules/execa/index.js:124:26)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async file:///Users/github/template-typescript-node-package/script/setup-test-e2e.js:38:20 {
  shortMessage: 'Command failed with exit code 2: grep --exclude script/setup.js --exclude script/setup-test-e2e.js --exclude-dir node_modules -i /JoshuaKGoldberg/ *.* **/*.*',
  command: 'grep --exclude script/setup.js --exclude script/setup-test-e2e.js --exclude-dir node_modules -i /JoshuaKGoldberg/ *.* **/*.*',
  escapedCommand: 'grep --exclude "script/setup.js" --exclude "script/setup-test-e2e.js" --exclude-dir node_modules -i "/JoshuaKGoldberg/" "*.*" "**/*.*"',
  exitCode: 2,
  signal: undefined,
  signalDescription: undefined,
  stdout: '',
  stderr: 'grep: *.*: No such file or directory\n' +
    'grep: **/*.*: No such file or directory',
  failed: true,
  timedOut: false,
  isCanceled: false,
  killed: false
}

Node.js v18.15.0

stderr: 'grep: *.*: No such file or directory\n' + 'grep: **/*.*: No such file or directory'

But running this directly in terminal runs well

grep --exclude script/setup.js --exclude script/setup-test-e2e.js --exclude-dir node_modules -i /JoshuaKGoldberg/ *.* **/*.*

@ehmicky
Copy link
Collaborator

ehmicky commented Apr 6, 2023

Thanks for the response @navin-moorthy,

It does not appear to be an error with Execa itself, but to confirm this, I would need to reproduce your setup. Would you it be possible for you to create a repository reproducing the above behavior? Thanks!

@navin-moorthy
Copy link
Author

@ehmicky, here is the file https://github.com/JoshuaKGoldberg/template-typescript-node-package/blob/main/script/setup-test-e2e.js that is in discussion.

You can checkout & run pnpm run setup:test to execute this file.

Additional Reference: JoshuaKGoldberg/create-typescript-app#357

@ehmicky
Copy link
Collaborator

ehmicky commented Apr 6, 2023

Hi @navin-moorthy,

I have tried to reproduce, but I am not encountering the same error as above. Would you be able to create a minimal GitHub repository which reproduces the above problem? Typically, it would only contain a single file to execute without any additional setup, with as few lines of code as possible to re-create the above error.

Thanks!

@navin-moorthy
Copy link
Author

navin-moorthy commented Apr 7, 2023

Here is the minimal reproduction in codesandbox - https://codesandbox.io/p/sandbox/quizzical-galois-bou50c?file=%2Findex.js

Github repo - https://github.com/navin-moorthy/execa-issue--555

Additional info,
Local machine - grep --version - grep (BSD grep, GNU compatible) 2.6.0-FreeBSD
Codesandbox - grep --version - grep (GNU grep) 3.6

@ehmicky
Copy link
Collaborator

ehmicky commented Apr 7, 2023

Thanks @navin-moorthy,

This example removes shell: true, so it fails but this is expected because the shell option is needed to interpret globbing. Otherwise it looks for a file literally named *.*.

const output = await $`grep --exclude-dir=node_modules/* -i template *.*  **/*.*`;
Error: Command failed with exit code 2: grep --exclude-dir=node_modules/* -i template *.* **/*.*
grep: *.*: No such file or directory
grep: **/*.*: No such file or directory

When you add shell: true, the example works correctly instead.

const output = await $({ shell: true })`grep --exclude-dir=node_modules/* -i template *.*  **/*.*`;
README.md:# Node.js template
index.js:// })`grep --exclude-dir=node_modules/* -i template *.*  **/*.*`;
index.js:const output = await $({ shell: true })`grep --exclude-dir=node_modules/* -i template *.*  **/*.*`;
package-lock.json:  "name": "templateforv2-static",
package-lock.json:      "name": "templateforv2-static",
package.json:  "name": "templateforv2-static",
package.json:    "build": "echo This is a static template, there is no bundler or bundling involved!"
package.json:    "url": "git+https://github.com/codesandbox-app/static-template.git"
package.json:    "url": "https://github.com/codesandbox-app/static-template/issues"
package.json:  "homepage": "https://github.com/codesandbox-app/static-template#readme",

@navin-moorthy
Copy link
Author

Thanks @ehmicky for the explanation 👍

@navin-moorthy
Copy link
Author

I found this quite useful using globby - https://github.com/google/zx/blob/main/docs/quotes.md#globbing-and-, https://github.com/google/zx/blob/main/README.md#globby-package

This solution works without {shell: true}.

const files = await globby(["*.*", "**/*.*"]);

const output = await $`grep --exclude-dir=node_modules/* -i ${files}`;

JoshuaKGoldberg added a commit to JoshuaKGoldberg/create-typescript-app that referenced this issue Apr 10, 2023
<!-- 👋 Hi, thanks for sending a PR to template-typescript-node-package!
💖.
Please fill out all fields below and make sure each item is true and [x]
checked.
Otherwise we may not be able to review your PR. -->

## PR Checklist

- [x] Addresses an existing open issue: fixes #357
- [x] That issue was marked as [`status: accepting
prs`](https://github.com/JoshuaKGoldberg/template-typescript-node-package/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22)
- [x] Steps in
[CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/template-typescript-node-package/blob/main/.github/CONTRIBUTING.md)
were taken

## Overview

<!-- Description of what is changed and how the code change does that.
-->
Added [`globby`](https://github.com/sindresorhus/globby) package. Reason
-
sindresorhus/execa#555 (comment)

---------

Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
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

No branches or pull requests

2 participants