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 reverting additions to PATH-like variables #4861

Merged
merged 3 commits into from
Oct 15, 2021
Merged

Conversation

dra27
Copy link
Member

@dra27 dra27 commented Oct 7, 2021

For environment variables which can be split (e.g. PATH on :), OpamEnv.unzip_to attempts to find the point in the variable at which a value was added. This is fine if a single value was added, but fails if the addition was multiple values (for example, if a setenv instruction added two directories to PATH in one +=).

This is fixed by first splitting the value being searched according to the same rule as the environmen variable and ensuring they all match in order.

  • Finalise test case
  • Double-check that it definitely fixes the Windows issue (I've rebased and edited it on Linux)

@dra27 dra27 added this to the 2.0.10 milestone Oct 7, 2021
@dra27
Copy link
Member Author

dra27 commented Oct 7, 2021

This opam file provides the basis for a test:

opam-version: "2.0"
license: "ISC"
synopsis: "Test package for setenv field"
description: "Test case for setenv field"
maintainer: "platform@lists.ocaml.org"
setenv: [
  [CAML_LD_LIBRARY_PATH += "%{_:doc}%:%{_:share}%"]
]
homepage: "https://opam.ocaml.org"
bug-reports: "https://github.com/ocaml/opam/issues"
authors: ["David Allsopp"]

This package needs to be installed on its own (or the ocaml package masks the problem) with opam switch create --repos=setenv-test-repo setenv-test --packages=setenv. With current opam opam env --revert will not include CAML_LD_LIBRARY_PATH (because opam fails to detect the need to revert the environment update at all) and with this PR it will include export CAML_LD_LIBRARY_PATH=''.

@rjbou - I wasn't sure where best to integrate this as a reftest... perhaps we can share some of the infrastructure used for the root-versions test in order to set-up a repository with just this package, or is there already an easier way in another test?

@dra27
Copy link
Member Author

dra27 commented Oct 8, 2021

The AppVeyor failure is serious, but not to do with this PR; the tests failures are to do with the gforge shutdown at Inria

@rjbou rjbou force-pushed the env-fix branch 2 times, most recently from 610dd0f to b66c9e0 Compare October 13, 2021 15:45
rjbou and others added 3 commits October 15, 2021 17:10
For environment variables which can be split (e.g. `PATH` on `:`),
OpamEnv.unzip_to attempts to find the point in the variable at which a
value was added. This is fine if a _single_ value was added, but fails
if the addition was multiple values (for example, if a setenv
instruction added two directories to `PATH` in one `+=`).

This is fixed by first splitting the value being searched according to
the same rule as the environmen variable and ensuring they all match in
order.
@rjbou rjbou merged commit f9c2bbf into ocaml:master Oct 15, 2021
This was referenced Oct 19, 2021
@dra27 dra27 deleted the env-fix branch April 25, 2024 11:16
dra27 added a commit to dra27/opam that referenced this pull request Apr 25, 2024
As in this commit, there should be no output captured from the
opam env --revert invocation.
dra27 added a commit to dra27/opam that referenced this pull request Apr 29, 2024
As in this commit, there should be no output captured from the
opam env --revert invocation.
dra27 added a commit to dra27/opam that referenced this pull request Apr 29, 2024
dra27 added a commit to dra27/opam that referenced this pull request Apr 29, 2024
When reverting [FOO += "value"], it is necessary to determine what to do
if "value" is of the form "<value1><sep><value2>". Because FOO itself
will have been split at <sep>, we will never match
"<value1><sep><value2>" so it is necessary to split value at <sep> as
well (this is the crux of ocaml#4861).

However, the interaction of this and x-env-path-rewrite is incorrect. If
x-env-path-rewrite has [FOO false] (the `norewrite case), then the value
must be split according to the default interpretation of FOO (which is
how it was added).

If x-env-path-rewrite has a rewrite rule for FOO, then opam has already
assumed that value refers to a single directory (the host/target and
quoting transformation options all being this way).

If x-env-path-rewrite does not have any rule (and does have have
[FOO false]) then we are in essence in the backwards-compatible case,
and must split value.

In order to stop poor interaction between this and opam itself
injecting Windows paths, opam's updates in OpamEnv (to MANPATH, etc.)
are explicitly expanded so that in unzip_to they are treated as though
they had an x-env-path-rewrite rule given (and are therefore not split).
dra27 added a commit to dra27/opam that referenced this pull request May 2, 2024
As in this commit, there should be no output captured from the
opam env --revert invocation.
dra27 added a commit to dra27/opam that referenced this pull request May 2, 2024
dra27 added a commit to dra27/opam that referenced this pull request May 2, 2024
The cases of this match were the wrong way around.

Note that this regresses ocaml#4861's quoted directory test.
dra27 added a commit to dra27/opam that referenced this pull request May 2, 2024
When reverting [FOO += "value"], it is necessary to determine what to do
if "value" is of the form "<value1><sep><value2>". Because FOO itself
will have been split at <sep>, we will never match
"<value1><sep><value2>" so it is necessary to split value at <sep> as
well (this is the crux of ocaml#4861).

However, the interaction of this and x-env-path-rewrite is incorrect. If
x-env-path-rewrite has [FOO false] (the `norewrite case), then the value
must be split according to the default interpretation of FOO (which is
how it was added).

If x-env-path-rewrite has a rewrite rule for FOO, then opam has already
assumed that value refers to a single directory (the host/target and
quoting transformation options all being this way).

If x-env-path-rewrite does not have any rule (and does have have
[FOO false]) then we are in essence in the backwards-compatible case,
and must split value.

In order to stop poor interaction between this and opam itself
injecting Windows paths, opam's updates in OpamEnv (to MANPATH, etc.)
are explicitly expanded so that in unzip_to they are treated as though
they had an x-env-path-rewrite rule given (and are therefore not split).

Combined with the previous fixes, this addresses ocaml#5838 without
regressing ocaml#4861.
rjbou pushed a commit to rjbou/opam that referenced this pull request May 10, 2024
rjbou pushed a commit to rjbou/opam that referenced this pull request May 10, 2024
The cases of this match were the wrong way around.

Note that this regresses ocaml#4861's quoted directory test.
rjbou pushed a commit to rjbou/opam that referenced this pull request May 10, 2024
When reverting [FOO += "value"], it is necessary to determine what to do
if "value" is of the form "<value1><sep><value2>". Because FOO itself
will have been split at <sep>, we will never match
"<value1><sep><value2>" so it is necessary to split value at <sep> as
well (this is the crux of ocaml#4861).

However, the interaction of this and x-env-path-rewrite is incorrect. If
x-env-path-rewrite has [FOO false] (the `norewrite case), then the value
must be split according to the default interpretation of FOO (which is
how it was added).

If x-env-path-rewrite has a rewrite rule for FOO, then opam has already
assumed that value refers to a single directory (the host/target and
quoting transformation options all being this way).

If x-env-path-rewrite does not have any rule (and does have have
[FOO false]) then we are in essence in the backwards-compatible case,
and must split value.

In order to stop poor interaction between this and opam itself
injecting Windows paths, opam's updates in OpamEnv (to MANPATH, etc.)
are explicitly expanded so that in unzip_to they are treated as though
they had an x-env-path-rewrite rule given (and are therefore not split).

Combined with the previous fixes, this addresses ocaml#5838 without
regressing ocaml#4861.
rjbou added a commit to rjbou/opam that referenced this pull request May 10, 2024
rjbou pushed a commit to rjbou/opam that referenced this pull request May 10, 2024
The cases of this match were the wrong way around.

Note that this regresses ocaml#4861's quoted directory test.
rjbou pushed a commit to rjbou/opam that referenced this pull request May 10, 2024
When reverting [FOO += "value"], it is necessary to determine what to do
if "value" is of the form "<value1><sep><value2>". Because FOO itself
will have been split at <sep>, we will never match
"<value1><sep><value2>" so it is necessary to split value at <sep> as
well (this is the crux of ocaml#4861).

However, the interaction of this and x-env-path-rewrite is incorrect. If
x-env-path-rewrite has [FOO false] (the `norewrite case), then the value
must be split according to the default interpretation of FOO (which is
how it was added).

If x-env-path-rewrite has a rewrite rule for FOO, then opam has already
assumed that value refers to a single directory (the host/target and
quoting transformation options all being this way).

If x-env-path-rewrite does not have any rule (and does have have
[FOO false]) then we are in essence in the backwards-compatible case,
and must split value.

In order to stop poor interaction between this and opam itself
injecting Windows paths, opam's updates in OpamEnv (to MANPATH, etc.)
are explicitly expanded so that in unzip_to they are treated as though
they had an x-env-path-rewrite rule given (and are therefore not split).

Combined with the previous fixes, this addresses ocaml#5838 without
regressing ocaml#4861.
rjbou pushed a commit to rjbou/opam that referenced this pull request May 10, 2024
As in this commit, there should be no output captured from the
opam env --revert invocation.
rjbou added a commit to rjbou/opam that referenced this pull request May 10, 2024
rjbou pushed a commit to rjbou/opam that referenced this pull request May 10, 2024
The cases of this match were the wrong way around.

Note that this regresses ocaml#4861's quoted directory test.
rjbou pushed a commit to rjbou/opam that referenced this pull request May 10, 2024
When reverting [FOO += "value"], it is necessary to determine what to do
if "value" is of the form "<value1><sep><value2>". Because FOO itself
will have been split at <sep>, we will never match
"<value1><sep><value2>" so it is necessary to split value at <sep> as
well (this is the crux of ocaml#4861).

However, the interaction of this and x-env-path-rewrite is incorrect. If
x-env-path-rewrite has [FOO false] (the `norewrite case), then the value
must be split according to the default interpretation of FOO (which is
how it was added).

If x-env-path-rewrite has a rewrite rule for FOO, then opam has already
assumed that value refers to a single directory (the host/target and
quoting transformation options all being this way).

If x-env-path-rewrite does not have any rule (and does have have
[FOO false]) then we are in essence in the backwards-compatible case,
and must split value.

In order to stop poor interaction between this and opam itself
injecting Windows paths, opam's updates in OpamEnv (to MANPATH, etc.)
are explicitly expanded so that in unzip_to they are treated as though
they had an x-env-path-rewrite rule given (and are therefore not split).

Combined with the previous fixes, this addresses ocaml#5838 without
regressing ocaml#4861.
rjbou pushed a commit to rjbou/opam that referenced this pull request May 10, 2024
As in this commit, there should be no output captured from the
opam env --revert invocation.
rjbou added a commit to rjbou/opam that referenced this pull request May 10, 2024
rjbou pushed a commit to rjbou/opam that referenced this pull request May 10, 2024
The cases of this match were the wrong way around.

Note that this regresses ocaml#4861's quoted directory test.
rjbou pushed a commit to rjbou/opam that referenced this pull request May 10, 2024
When reverting [FOO += "value"], it is necessary to determine what to do
if "value" is of the form "<value1><sep><value2>". Because FOO itself
will have been split at <sep>, we will never match
"<value1><sep><value2>" so it is necessary to split value at <sep> as
well (this is the crux of ocaml#4861).

However, the interaction of this and x-env-path-rewrite is incorrect. If
x-env-path-rewrite has [FOO false] (the `norewrite case), then the value
must be split according to the default interpretation of FOO (which is
how it was added).

If x-env-path-rewrite has a rewrite rule for FOO, then opam has already
assumed that value refers to a single directory (the host/target and
quoting transformation options all being this way).

If x-env-path-rewrite does not have any rule (and does have have
[FOO false]) then we are in essence in the backwards-compatible case,
and must split value.

In order to stop poor interaction between this and opam itself
injecting Windows paths, opam's updates in OpamEnv (to MANPATH, etc.)
are explicitly expanded so that in unzip_to they are treated as though
they had an x-env-path-rewrite rule given (and are therefore not split).

Combined with the previous fixes, this addresses ocaml#5838 without
regressing ocaml#4861.
rjbou pushed a commit to rjbou/opam that referenced this pull request May 10, 2024
As in this commit, there should be no output captured from the
opam env --revert invocation.
rjbou pushed a commit to rjbou/opam that referenced this pull request May 10, 2024
rjbou pushed a commit to rjbou/opam that referenced this pull request May 10, 2024
The cases of this match were the wrong way around.

Note that this regresses ocaml#4861's quoted directory test.
rjbou pushed a commit to rjbou/opam that referenced this pull request May 10, 2024
When reverting [FOO += "value"], it is necessary to determine what to do
if "value" is of the form "<value1><sep><value2>". Because FOO itself
will have been split at <sep>, we will never match
"<value1><sep><value2>" so it is necessary to split value at <sep> as
well (this is the crux of ocaml#4861).

However, the interaction of this and x-env-path-rewrite is incorrect. If
x-env-path-rewrite has [FOO false] (the `norewrite case), then the value
must be split according to the default interpretation of FOO (which is
how it was added).

If x-env-path-rewrite has a rewrite rule for FOO, then opam has already
assumed that value refers to a single directory (the host/target and
quoting transformation options all being this way).

If x-env-path-rewrite does not have any rule (and does have have
[FOO false]) then we are in essence in the backwards-compatible case,
and must split value.

In order to stop poor interaction between this and opam itself
injecting Windows paths, opam's updates in OpamEnv (to MANPATH, etc.)
are explicitly expanded so that in unzip_to they are treated as though
they had an x-env-path-rewrite rule given (and are therefore not split).

Combined with the previous fixes, this addresses ocaml#5838 without
regressing ocaml#4861.
dra27 added a commit to dra27/opam that referenced this pull request May 14, 2024
As in this commit, there should be no output captured from the
opam env --revert invocation.
dra27 added a commit to dra27/opam that referenced this pull request May 14, 2024
dra27 added a commit to dra27/opam that referenced this pull request May 14, 2024
The cases of this match were the wrong way around.

Note that this regresses ocaml#4861's quoted directory test.
dra27 added a commit to dra27/opam that referenced this pull request May 14, 2024
When reverting [FOO += "value"], it is necessary to determine what to do
if "value" is of the form "<value1><sep><value2>". Because FOO itself
will have been split at <sep>, we will never match
"<value1><sep><value2>" so it is necessary to split value at <sep> as
well (this is the crux of ocaml#4861).

However, the interaction of this and x-env-path-rewrite is incorrect. If
x-env-path-rewrite has [FOO false] (the `norewrite case), then the value
must be split according to the default interpretation of FOO (which is
how it was added).

If x-env-path-rewrite has a rewrite rule for FOO, then opam has already
assumed that value refers to a single directory (the host/target and
quoting transformation options all being this way).

If x-env-path-rewrite does not have any rule (and does have have
[FOO false]) then we are in essence in the backwards-compatible case,
and must split value.

In order to stop poor interaction between this and opam itself
injecting Windows paths, opam's updates in OpamEnv (to MANPATH, etc.)
are explicitly expanded so that in unzip_to they are treated as though
they had an x-env-path-rewrite rule given (and are therefore not split).

Combined with the previous fixes, this addresses ocaml#5838 without
regressing ocaml#4861.
dra27 added a commit to dra27/opam that referenced this pull request May 14, 2024
When reverting [FOO += "value"], it is necessary to determine what to do
if "value" is of the form "<value1><sep><value2>". Because FOO itself
will have been split at <sep>, we will never match
"<value1><sep><value2>" so it is necessary to split value at <sep> as
well (this is the crux of ocaml#4861).

However, the interaction of this and x-env-path-rewrite is incorrect. If
x-env-path-rewrite has [FOO false] (the `norewrite case), then the value
must be split according to the default interpretation of FOO (which is
how it was added).

If x-env-path-rewrite has a rewrite rule for FOO, then opam has already
assumed that value refers to a single directory (the host/target and
quoting transformation options all being this way).

If x-env-path-rewrite does not have any rule (and does have have
[FOO false]) then we are in essence in the backwards-compatible case,
and must split value.

In order to stop poor interaction between this and opam itself
injecting Windows paths, opam's updates in OpamEnv (to MANPATH, etc.)
are explicitly expanded so that in unzip_to they are treated as though
they had an x-env-path-rewrite rule given (and are therefore not split).

Combined with the previous fixes, this addresses ocaml#5838 without
regressing ocaml#4861.
dra27 added a commit to dra27/opam that referenced this pull request May 14, 2024
The cases of this match were the wrong way around.

Note that this regresses ocaml#4861's quoted directory test.
dra27 added a commit to dra27/opam that referenced this pull request May 14, 2024
When reverting [FOO += "value"], it is necessary to determine what to do
if "value" is of the form "<value1><sep><value2>". Because FOO itself
will have been split at <sep>, we will never match
"<value1><sep><value2>" so it is necessary to split value at <sep> as
well (this is the crux of ocaml#4861).

However, the interaction of this and x-env-path-rewrite is incorrect. If
x-env-path-rewrite has [FOO false] (the `norewrite case), then the value
must be split according to the default interpretation of FOO (which is
how it was added).

If x-env-path-rewrite has a rewrite rule for FOO, then opam has already
assumed that value refers to a single directory (the host/target and
quoting transformation options all being this way).

If x-env-path-rewrite does not have any rule (and does have have
[FOO false]) then we are in essence in the backwards-compatible case,
and must split value.

In order to stop poor interaction between this and opam itself
injecting Windows paths, opam's updates in OpamEnv (to MANPATH, etc.)
are explicitly expanded so that in unzip_to they are treated as though
they had an x-env-path-rewrite rule given (and are therefore not split).

Combined with the previous fixes, this addresses ocaml#5838 without
regressing ocaml#4861.
dra27 added a commit to dra27/opam that referenced this pull request May 15, 2024
As in this commit, there should be no output captured from the
opam env --revert invocation.
dra27 added a commit to dra27/opam that referenced this pull request May 15, 2024
dra27 added a commit to dra27/opam that referenced this pull request May 15, 2024
The cases of this match were the wrong way around.

Note that this regresses ocaml#4861's quoted directory test.
dra27 added a commit to dra27/opam that referenced this pull request May 15, 2024
When reverting [FOO += "value"], it is necessary to determine what to do
if "value" is of the form "<value1><sep><value2>". Because FOO itself
will have been split at <sep>, we will never match
"<value1><sep><value2>" so it is necessary to split value at <sep> as
well (this is the crux of ocaml#4861).

However, the interaction of this and x-env-path-rewrite is incorrect. If
x-env-path-rewrite has [FOO false] (the `norewrite case), then the value
must be split according to the default interpretation of FOO (which is
how it was added).

If x-env-path-rewrite has a rewrite rule for FOO, then opam has already
assumed that value refers to a single directory (the host/target and
quoting transformation options all being this way).

If x-env-path-rewrite does not have any rule (and does have have
[FOO false]) then we are in essence in the backwards-compatible case,
and must split value.

In order to stop poor interaction between this and opam itself
injecting Windows paths, opam's updates in OpamEnv (to MANPATH, etc.)
are explicitly expanded so that in unzip_to they are treated as though
they had an x-env-path-rewrite rule given (and are therefore not split).

Combined with the previous fixes, this addresses ocaml#5838 without
regressing ocaml#4861.
@dra27 dra27 mentioned this pull request Jun 13, 2024
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

Successfully merging this pull request may close these issues.

2 participants