diff --git a/src/client/opamConfigCommand.ml b/src/client/opamConfigCommand.ml index 4937cde29a6..67637ccbc8f 100644 --- a/src/client/opamConfigCommand.ml +++ b/src/client/opamConfigCommand.ml @@ -315,32 +315,35 @@ let set_global var value = OpamGlobalState.write { gt with config } let variable gt v = - match OpamPackageVar.resolve_global gt v with - | Some c -> - OpamConsole.msg "%s\n" (OpamVariable.string_of_variable_contents c) - | None -> + let raw_switch_content = match OpamStateConfig.get_switch_opt () with - | None -> - OpamConsole.error_and_exit `Not_found - "Variable %s not found" - (OpamVariable.Full.to_string v) | Some switch -> let switch_config = OpamFile.Switch_config.safe_read (OpamPath.Switch.switch_config gt.root switch) in - match OpamPackageVar.resolve_switch_raw gt switch switch_config v with - | Some c -> - OpamConsole.msg "%s\n" (OpamVariable.string_of_variable_contents c) - | None -> - OpamSwitchState.with_ `Lock_none gt @@ fun st -> - match OpamPackageVar.resolve st v with - | Some c -> - OpamConsole.msg "%s\n" (OpamVariable.string_of_variable_contents c) - | None -> - OpamConsole.error_and_exit `Not_found - "Variable %s not found" - (OpamVariable.Full.to_string v) + OpamPackageVar.resolve_switch_raw gt switch switch_config v + | None -> None + in + let switch_content = + match raw_switch_content with + | None when not (OpamVariable.Full.is_global v) -> + OpamSwitchState.with_ `Lock_none gt @@ fun st -> + OpamPackageVar.resolve st v + | rsc -> rsc + in + let content = + match switch_content with + | Some _ as some -> some + | None -> OpamPackageVar.resolve_global gt v + in + match content with + | Some c -> OpamConsole.msg "%s\n" (OpamVariable.string_of_variable_contents c) + | None -> + OpamConsole.error_and_exit `Not_found + "Variable %s not found" + (OpamVariable.Full.to_string v) + let exec gt ?set_opamroot ?set_opamswitch ~inplace_path command = log "config-exec command=%a" (slog (String.concat " ")) command;