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

Unable to suppress "source" errors #738

Closed
Zamiell opened this issue Feb 22, 2023 · 16 comments
Closed

Unable to suppress "source" errors #738

Zamiell opened this issue Feb 22, 2023 · 16 comments

Comments

@Zamiell
Copy link

Zamiell commented Feb 22, 2023

Code editor

VSCode

Platform

Windows

Version

1.33.0

What steps will reproduce the bug?

Create a new repository:

mkdir test
cd test
touch .env.example
touch test.sh
code .

Fill .env.exmaple with the following:

FOO=""

Fill test.sh with the following:

#!/bin/bash

# Get the directory of this script:
# https://stackoverflow.com/questions/59895/getting-the-source-directory-of-a-bash-script-from-within
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

# shellcheck source=.env.example
source "$DIR/.env"

Upon doing this, we get two errors in the IDE:

First:

Source command could not be analyzed: non-constant source not supported.

Note that enabling the configuration flag "includeAllWorkspaceSymbols"
would include all symbols in the workspace regardless of source commands.
But be aware that this will lead to false positive suggestions.

It seems that the language server isn't able to interpret shellcheck comment directives. Is there some other way to suppress the error?

Second:

Not following: .env.example was not specified as input (see shellcheck -x).

This error message is confusing, because the -x is short for --external-sources, but it should be already set by the extension.

What is the expected behavior?

I expect there to be no error messages in the "Problems" pane of VSCode.

What do you see instead?

I see two errors and it is unclear how to solve / suppress them.

Additional Info

I have shellcheck installed, so I presume the extension is automatically picking it up and using it to lint.

@misaki-web
Copy link

There's also the error "Source command could not be analyzed: failed to resolve path" even if the file is sourced conditionally. Example:

#!/bin/bash

cd -- "$(dirname -- "${BASH_SOURCE[0]}")" || exit 1

if [[ -f "./conf.sh" ]]; then
	# shellcheck disable=SC1091
	source "./conf.sh"
fi

Error:

Source command could not be analyzed: failed to resolve path.

Note that enabling the configuration flag "includeAllWorkspaceSymbols"
would include all symbols in the workspace regardless of source commands.
But be aware that this will lead to false positive suggestions.

@skovhus
Copy link
Collaborator

skovhus commented Feb 24, 2023

Thanks for reporting this. Contributions are more than welcome.

@lamyergeier
Copy link

@skovhus Is it possible to hide all Problems reported by the extension? I use shell check extension separately which does the job perfectly.

@skovhus
Copy link
Collaborator

skovhus commented Mar 1, 2023

Note that the warning “Source command could not be analyzed” isn’t related to shellcheck, it informs the user that jump to definition and documentation across files wouldn’t work.

The warning was added to inform the user why these features aren’t working. But I’m considering if we should just disable them.

@ZiViZiViZ
Copy link

Is there a way to config the LSP to disable this "source command" check?

@skovhus
Copy link
Collaborator

skovhus commented Mar 4, 2023

We are now parsing shellcheck directives in version 1.34 of the vscode extension and the server version 4.8.1.

Let me know if this works for you, and if you have further ideas for improving this (or the extension/server in general). Thanks!

@skovhus skovhus closed this as completed Mar 4, 2023
@Zamiell
Copy link
Author

Zamiell commented Mar 5, 2023

Hello, and thanks for the update. However, the extension still doesn't work properly. After following the steps in the OP, I get the following error:

image

@Zamiell
Copy link
Author

Zamiell commented Mar 5, 2023

Can you reopen this issue?

@skovhus
Copy link
Collaborator

skovhus commented Mar 5, 2023

After following the steps in the OP, I get the following error:

OP? What exactly did you do? Please share more context and the disable directive you used?

@skovhus skovhus reopened this Mar 5, 2023
@skovhus
Copy link
Collaborator

skovhus commented Mar 5, 2023

A little demo to show how it works in vscode on my machine:
Mar-05-2023 19-49-23

@Zamiell
Copy link
Author

Zamiell commented Mar 6, 2023

OP is short for "original post".
I'm on Windows 10, so I followed the exact steps in the original post on this thread/issue.

@skovhus
Copy link
Collaborator

skovhus commented Mar 6, 2023

I'm on Windows 10, so I followed the exact steps in the original post on this thread/issue.

Thanks. Can you verify which version of the extension you are using?

@lamyergeier
Copy link

@skovhus

In the following snippet I get warning as shown below. The warning goes away if I change . TestExist || exit to . TestExist


  • Code
	#shellcheck source=/dev/null
	. TestExist || exit

  • Getting following in Problems Tab of Vscode

    Source command could not be analyzed: failed to resolve path.
    
    Consider adding a ShellCheck directive above this line to fix or ignore this:
    # shellcheck source=/my-file.sh # specify the file to source
    # shellcheck source-path=my_script_folder # specify the folder to search in
    # shellcheck source=/dev/null # to ignore the error
    

@misaki-web
Copy link

We are now parsing shellcheck directives in version 1.34 of the vscode extension and the server version 4.8.1.

Let me know if this works for you, and if you have further ideas for improving this (or the extension/server in general). Thanks!

Testing v1.35.0, shellcheck directives are parsed, but only if they're just above the line. Directives applying to the entire file are ignored:

Directives that replace or are immediately after the shebang apply to the entire script. Otherwise, they are scoped to the command that follows it (including compound commands like function definitions, loops and case statements).

For example, the following script generates 3 bashIde warnings and 3 shellcheck warnings:

#!/bin/bash

cd -- "$(dirname -- "${BASH_SOURCE[0]}")" || exit 1

if [[ -f "./file.sh" ]]; then
	source "./file.sh"
fi

if [[ -f "./file2.sh" ]]; then
	source "./file2.sh"
fi

if [[ -f "./file3.sh" ]]; then
	source "./file3.sh"
fi

One directive at the top of the file allows to disable all shellcheck warnings, but bashIde still displays its own warnings:

#!/bin/bash

# shellcheck disable=SC1091

cd -- "$(dirname -- "${BASH_SOURCE[0]}")" || exit 1

if [[ -f "./file.sh" ]]; then
	source "./file.sh"
fi

if [[ -f "./file2.sh" ]]; then
	source "./file2.sh"
fi

if [[ -f "./file3.sh" ]]; then
	source "./file3.sh"
fi

@skovhus
Copy link
Collaborator

skovhus commented Mar 24, 2023

FYI I've made the "Source command could not be analyzed" diagnostics message configurable and disabled by default. See #794 – this was released as vscode extension 1.36 and bash-language-server version 4.8.4. I'm contemplating if the message should be on by default, as it should help the user add ShellCheck directives to resolve most issues...

@misaki-web
Copy link

FYI I've made the "Source command could not be analyzed" diagnostics message configurable and disabled by default. See #794 – this was released as vscode extension 1.36 and bash-language-server version 4.8.4. I'm contemplating if the message should be on by default, as it should help the user add ShellCheck directives to resolve most issues...

It works great. Thanks a lot. IMO it could be enabled by default, especially since the error message contains a directive on how to disable it.

@skovhus skovhus closed this as completed Jul 15, 2023
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

5 participants