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

Fix issues reported by shellcheck #5910

Merged
merged 15 commits into from
Oct 7, 2022

Conversation

pavoljuhas
Copy link
Collaborator

@pavoljuhas pavoljuhas commented Oct 5, 2022

  • exit on cd failure, SC2164
  • needless double negative, SC2236
  • fix useless use of cat, SC2002
  • replace legacy backticks, SC2006
  • clean unused variables, SC2034
  • avoid globing/splitting on variable expansion, SC2086
  • expand variables at trap execution, SC2064
  • remove redundant loop over single value, SC2043
  • export variable separately from value assignment, SC2155
  • ${name} is not needed in arithmetic expressions, SC2004
  • do not assign array to string, SC2124
  • disable shellcheck for zsh block
  • add shebang to dev_tools/pypath to identify it as a bash script

No change in code logic.
The updated code passes the following test

$ git ls-files | xargs file | grep -i shell.script | cut -d: -f1 |
      xargs shellcheck

@pavoljuhas pavoljuhas requested review from a team, vtomole and cduck as code owners October 5, 2022 21:50
@pavoljuhas pavoljuhas requested a review from dabacon October 5, 2022 21:51
@CirqBot CirqBot added the size: L 250< lines changed <1000 label Oct 5, 2022
@@ -31,8 +31,9 @@
################################################################################

# Get the working directory to the repo root.
cd "$(dirname "${BASH_SOURCE[0]}")"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not cd "$(dirname "${BASH_SOURCE[0]}")" || exit?

My local shellcheck says ^-- SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

Copy link
Collaborator

Choose a reason for hiding this comment

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

That's what we are doing, i'm just curious about the creation of the thisdir variable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Why not cd "$(dirname "${BASH_SOURCE[0]}")"

Such cd would still pass for a failing $(...) command. If that command had no standard output,
the shell would expand it as cd "" and stay in the same working directory.
That could have unexpected effects further below.

Here is how to test -

$ cd /tmp
$ cd "$(false)" || echo "cd failed"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's what we are doing, i'm just curious about the creation of the thisdir variable.

This allows us to fail when $(...) fails -

$ thisdir=$(false) || echo directory expression failed

@@ -21,6 +21,7 @@

Copy link
Collaborator

Choose a reason for hiding this comment

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

When i run this file

(cirq_venv) vtomole@vtomole:~/Cirq$ shellcheck dev_tools/pypath 

In dev_tools/pypath line 1:
# Copyright 2018 The Cirq Developers
^-- SC2148: Tips depend on target shell and yours is unknown. Add a shebang.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The dev_tools/pypath should work with both bash and zsh.
We can check it using

$ shellcheck --shell=bash dev_tools/pypath

Here is how I test all files, dev_tools/pypath is added separately, because file does not identify it as a shell script:

$ git ls-files | xargs file | grep Bourne-Again.shell.script | cut -d: -f1 |
      xargs shellcheck --shell=bash dev_tools/pypath

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we add a shebang at the top so that it is identified as a shell script?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point! Done in c44b6bf.

@pavoljuhas
Copy link
Collaborator Author

@vtomole - thank you for the review! PTAL again at your convenience.

Help shellcheck to recognize bash syntax in dev_tools/pypath.

Note the executable flag on `dev_tools/pypath` is deliberately off.
The file is intended for sourcing from other shell sessions.
Copy link
Collaborator

@vtomole vtomole left a comment

Choose a reason for hiding this comment

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

LGTM

@pavoljuhas pavoljuhas added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Oct 7, 2022
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Oct 7, 2022
@CirqBot CirqBot merged commit df7d313 into quantumlib:master Oct 7, 2022
@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Oct 7, 2022
@pavoljuhas pavoljuhas deleted the appease-shellcheck branch October 7, 2022 18:22
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
- exit on cd failure, SC2164
- needless double negative, SC2236
- fix useless use of cat, SC2002
- replace legacy backticks, SC2006
- clean unused variables, SC2034
- avoid globing/splitting on variable expansion, SC2086
- expand variables at trap execution, SC2064
- remove redundant loop over single value, SC2043
- export variable separately from value assignment, SC2155
- ${name} is not needed in arithmetic expressions, SC2004
- do not assign array to string, SC2124
- disable shellcheck for zsh block
- add shebang to `dev_tools/pypath` to identify it as a bash script

No change in code logic.
The updated code passes the following test

```
$ git ls-files | xargs file | grep -i shell.script | cut -d: -f1 |
      xargs shellcheck
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: L 250< lines changed <1000
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants