docker / buildx

Docker CLI plugin for extended build capabilities with BuildKit
Apache License 2.0
3.59k stars 483 forks source link

UndefinedVar build check behaviour for PATH-like variables #2751

Open Stikus opened 1 month ago

Stikus commented 1 month ago

According to docs:

Before you reference an environment variable or a build argument in a RUN instruction, you should ensure that the variable is declared in the Dockerfile, using the ARG or ENV instructions.

This is not correct statement - you can append $PATH without any declaration in Dockerfile, as well as some other variables like $LD_LIBRARY_PATH. But not PYTHONPATH:

PYTHONPATH="$SOFT/Stranger-${STRANGER_VERSION}/local/lib/python3.10/dist-packages:$PYTHONPATH"

results warning: UndefinedVar: Usage of undefined variable '$PYTHONPATH' (line 72)

Are there any settings for predefined variables (not in Dockerfile) or how is should be fixed?

Stack Overflow question about this problem.

crazy-max commented 1 month ago

you can append $PATH without any declaration in Dockerfile, as well as some other variables like $LD_LIBRARY_PATH

It depends if this ENV has already been declared in the base image.

results warning: UndefinedVar: Usage of undefined variable '$PYTHONPATH' (line 72)

This means PYTHONPATH has not been declared in your Dockerfile or the base image you're using.

Stikus commented 1 month ago

Does this check actually checks base image? Documentation doesn't have any info about it.

Is there any way to ignore only current case of this check (not all check entirely)? Something like https://github.com/hadolint/hadolint?tab=readme-ov-file#inline-ignores or https://github.com/koalaman/shellcheck/wiki/Ignore#ignoring-one-specific-instance-in-a-file

crazy-max commented 1 month ago

Does this check actually checks base image?

Yes it does

Documentation doesn't have any info about it.

Indeed we could add a callout about it (cc @dvdksn).

Is there any way to ignore only current case of this check (not all check entirely)? Something like https://github.com/hadolint/hadolint?tab=readme-ov-file#inline-ignores or https://github.com/koalaman/shellcheck/wiki/Ignore#ignoring-one-specific-instance-in-a-file

I recall this has been discussed internally but not sure what is the current state of it. @colinhemmings do we have this scoped to a milestone?

colinhemmings commented 1 month ago

@Stikus, yes, we have something similar to inline ignores on the roadmap, but we don't have a date for it just yet. I will make sure to keep you posted.

Stikus commented 1 month ago

Thanks for fast answer, maybe there are any workarounds - like PATH1=${PATH1:-}:/new-path1 or PYTHONPATH="$SOFT/Stranger-${STRANGER_VERSION}/local/lib/python3.10/dist-packages:${PYTHONPATH:-''}"? To ensure that user is aware about empty variable?

serge2016 commented 1 month ago

Hello! I am also interested in workarounds... Which one is better?