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

Path rewriting for setenv: and build-env: #5636

Merged
merged 14 commits into from
Nov 13, 2023
Merged

Conversation

rjbou
Copy link
Collaborator

@rjbou rjbou commented Aug 17, 2023

Add environment path rewriting for Windows, see spec for more details.

New x- field x-env-path-rewrite to define rewriting rules, in the format: IDENT <bool> | (<separator-with-filter-formula> <format-with-filter-formula>) list

x-env-path-rewrite: [
  [ VAR1 false ]
  [ VAR2 ":" "host"]
  [ VAR3 (":" | ";" {os=win32}) ("target" | "target-quoted" {os=win32})]
]

host will call cygpath on the added path on windows. *-quoted will quote the added path if it contains the separator. target, if given explicitly will rewrite slashes in bacwardslashes.

It has an effect on environment file, as it needs to store the resolved separator and format, but it remains backward compatible:

VAR1 = path/to/add
VAR2 = path/to/add comment
VAR3 = path/to/add : host comment
VAR4 = path/to/add : host comment

todo:

  • update changes
  • update doc
  • add formula not resolved error case
  • update effectively equal to check x-env-path-rewrite

Old Version
Implementation of proposal described in #5602
+ add lint to check that the x- field is well defined
+ add lint that check that if x- field is true, an opam-version availability guard is set

todo:

  • add proper test
  • handle & check no modification on parse/print
  • update changes
  • look for another name
  • remove opam minimal version

@rjbou rjbou added the PR: WIP Not for merge at this stage label Aug 17, 2023
@rjbou rjbou added this to the 2.2.0~alpha3 milestone Aug 17, 2023
src/format/opamFile.ml Outdated Show resolved Hide resolved
@rjbou rjbou added PR: WAITING FOR REVIEW and removed PR: WIP Not for merge at this stage labels Aug 25, 2023
src/state/opamFileTools.ml Outdated Show resolved Hide resolved
src/state/opamFileTools.ml Outdated Show resolved Hide resolved
@rjbou
Copy link
Collaborator Author

rjbou commented Aug 29, 2023

Updated

src/format/opamFile.ml Outdated Show resolved Hide resolved
src/format/opamFile.ml Outdated Show resolved Hide resolved
Copy link
Member

@AltGr AltGr left a comment

Choose a reason for hiding this comment

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

LGTM, but... where is the doc ?
In it it may also be appropriate to mention the reason for an x-* field (the manual specifies that it's normally for external tools IIRC)

src/format/opamFile.ml Outdated Show resolved Hide resolved
src/state/opamFileTools.ml Outdated Show resolved Hide resolved
@rjbou
Copy link
Collaborator Author

rjbou commented Sep 7, 2023

To not change the opam file syntax, opam need to use it's own external tool features ^^

@rjbou
Copy link
Collaborator Author

rjbou commented Sep 7, 2023

Doc updated!

src/format/opamFile.ml Outdated Show resolved Hide resolved
src/format/opamFile.ml Outdated Show resolved Hide resolved
Copy link
Member

@dra27 dra27 left a comment

Choose a reason for hiding this comment

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

I'm just testing locally with the various packages that everything continues to work as needed, but I'm not expecting to find a problem, thank you!

Various suggestions, most of them documentation/comments, but it's looking lovely 🙂

doc/pages/Manual.md Outdated Show resolved Hide resolved
doc/pages/Manual.md Outdated Show resolved Hide resolved
doc/pages/Manual.md Outdated Show resolved Hide resolved
envu_comment = None;
envu_rewrite = empty;
}] |>
(* XXX Rewrite rules ?? *)
Copy link
Member

Choose a reason for hiding this comment

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

Stray comment?

envu_value = OpamFilename.Dir.to_string cygbin;
envu_comment = Some "Cygwin path";
envu_rewrite = empty;
}]
Copy link
Member

Choose a reason for hiding this comment

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

Given that this happens a lot, worth having OpamBaseTypes.env_update:

let env_update ?comment:envu_comment envu_var envu_op envu_value rewrite =
  let envu_rewrite = match rewrite with
  | `resolved -> Some (SPF_Resolved None)
  | `unresolved -> Some (SPF_Unresolved (Empty, Empty))
  | `rewrite r -> r in
  {envu_var; envu_op; envu_value; envu_comment; envu_rewrite}

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added in a commit

Copy link
Collaborator Author

@rjbou rjbou Nov 10, 2023

Choose a reason for hiding this comment

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

I don't know which version is more easily readable

      [{ envu_var = "PATH";
         envu_op = EqPlus;
         envu_value = OpamFilename.Dir.to_string cygbin;
         envu_comment = Some "Cygwin path";
         envu_rewrite = empty;
       }]

vs

      [ OpamTypesBase.env_update_resolved ~var:"PATH" ~op:EqPlus
          ~value:(OpamFilename.Dir.to_string cygbin)
          ~comment:"Cygwin path" () ]

Copy link
Member

Choose a reason for hiding this comment

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

Sorry - looking at it again, I now realise that I'd forgotten that a GADT was involved and that we can't convey that in optional parameters! (yet... it's been on my wish list for quite a few years...)

Wouldn't it be OK not to label those three arguments, though> I think env_update_resolved "PATH" EqPlus (OpamFilename.Dir.to_string cygbin quite clearly conveys what it's doing to give:

      [ OpamTypesBase.env_update_resolved ~comment:"Cygwin path"
          "PATH" EqPlus (OpamFilename.Dir.to_string cygbin) ]

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It is possible. The labels came at first because of var & values are string, and like that there is no confusion between them.
I'm ok to remove them and have then

val env_update_resolved:
  ?comment:string -> ?rewrite:spf_resolved separator_path_format option
  -> string -> env_update_op_kind -> string
  -> spf_resolved env_update

src/format/opamFile.ml Show resolved Hide resolved
doc/pages/Manual.md Outdated Show resolved Hide resolved
doc/pages/Manual.md Outdated Show resolved Hide resolved
@rjbou rjbou force-pushed the setenv-slash branch 2 times, most recently from d288ab4 to 611f497 Compare November 13, 2023 11:48
type env_update = {
  envu_var : string;
  envu_op : OpamParserTypes.FullPos.env_update_op_kind;
  envu_value : string;
  envu_comment : string option;
}
It contains rewriting rules of the environment variables to update:
- not a path, no rewrite
- formulaes to resolve to retrieve separator & path format
- resolved formulaes, directly separator & path format

This commit contains only the addition of the field, it does not
performs the rewriting.
…ewriting rules for variables defined in `setenv` and `build-env`.

Format is
x-env-path-rewrite: [
  [ VAR false ] # no a path, no rewrite
  [ VAR ":" "target" ] # always use ":" and target format
  [ VAR (":" | ";" { os=win32 }) ("target-quoted" | "target" { os = win32}) ]
  # On windows use ";" separator and don't quote, otherwise ":" and quote the set/added path if needed.
]
It keeps backward compatibility.

Format is then:
| VAR OP VALUE
| VAR OP VALUE COMMENT
| VAR OP VALUE norewrite COMMENT
| VAR OP VALUE norewrite
| VAR OP VALUE SEPARATOR PATH-FORMAT
| VAR OP VALUE SEPARATOR PATH-FORMAT COMMENT
…t per variable) to compute environment updates
@kit-ty-kate
Copy link
Member

Thanks!

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.

Variable expansion in env fields on Windows Opam setenv for path lists is broken on Windows/Cygwin
4 participants