hansva commented on code in PR #9289:
URL: https://github.com/apache/hop/pull/9289#discussion_r4536534664


##########
plugins/misc/projects/src/main/java/org/apache/hop/projects/environment/ManageEnvironmentsOptionPlugin.java:
##########
@@ -243,24 +243,36 @@ private void createEnvironment(
             + HopConfig.getInstance().getConfigFilename());
 
     enableProject(log, environment, variables, hasHopMetadataProvider, config);
-
-    validateConfigFiles(log, variables, environment);
     logEnvironmentDetails(log, environment);
   }
 
+  /**
+   * Create the environment's missing configuration files, then enable its 
project with them.
+   *
+   * <p>The project variables are applied before the files are created, since 
a configuration file
+   * path can refer to them (for example '${PROJECT_HOME}'). Enabling the 
project reads the files,
+   * so they have to exist by then.
+   */
   private void enableProject(
       ILogChannel log,
       LifecycleEnvironment environment,
       IVariables variables,
       IHasHopMetadataProvider hasHopMetadataProvider,
       ProjectsConfig config)
-      throws HopException {
+      throws Exception {
     ProjectConfig projectConfig = 
config.findProjectConfig(environment.getProjectName());
     if (projectConfig == null) {
       log.logBasic(
           "Warning: referenced project '" + environment.getProjectName() + "' 
doesn't exist");
+      validateConfigFiles(log, variables, environment);
     } else {
       Project project = projectConfig.loadProject(variables);
+      // Project variables only, so '${PROJECT_HOME}' in the configuration 
file paths resolves.

Review Comment:
   This fixes the ordering (config files must exist before they are read) at 
the hop-conf call site. Other callers of `ProjectsUtil.enableProject()` with 
not-yet-existing environment config files (GUI environment dialog, `hop run 
-e`) still get the original "does not exist" error. Would it make sense to move 
this into `ProjectsUtil.enableProject()` so there's one code path?



##########
plugins/misc/projects/src/main/java/org/apache/hop/projects/environment/ManageEnvironmentsOptionPlugin.java:
##########
@@ -243,24 +243,36 @@ private void createEnvironment(
             + HopConfig.getInstance().getConfigFilename());
 
     enableProject(log, environment, variables, hasHopMetadataProvider, config);

Review Comment:
   The environment is added and saved to hop-config.json (line 236-237) before 
`enableProject()` runs, and `enableProject()` can now throw (`modifyVariables`, 
the parent chain check, creating config files). Nothing rolls the environment 
back here, unlike the rollback this PR adds for project create.
   
   Example: `hop conf -ec -e dev -ep p -eg /readonly/dev.json` fails, but `dev` 
stays registered, and a retry fails with "Environment 'dev' already exists".



##########
plugins/misc/projects/src/main/java/org/apache/hop/projects/environment/ManageEnvironmentsOptionPlugin.java:
##########
@@ -243,24 +243,36 @@ private void createEnvironment(
             + HopConfig.getInstance().getConfigFilename());
 
     enableProject(log, environment, variables, hasHopMetadataProvider, config);
-
-    validateConfigFiles(log, variables, environment);
     logEnvironmentDetails(log, environment);
   }
 
+  /**
+   * Create the environment's missing configuration files, then enable its 
project with them.
+   *
+   * <p>The project variables are applied before the files are created, since 
a configuration file
+   * path can refer to them (for example '${PROJECT_HOME}'). Enabling the 
project reads the files,
+   * so they have to exist by then.
+   */
   private void enableProject(
       ILogChannel log,
       LifecycleEnvironment environment,
       IVariables variables,
       IHasHopMetadataProvider hasHopMetadataProvider,
       ProjectsConfig config)
-      throws HopException {
+      throws Exception {
     ProjectConfig projectConfig = 
config.findProjectConfig(environment.getProjectName());
     if (projectConfig == null) {
       log.logBasic(
           "Warning: referenced project '" + environment.getProjectName() + "' 
doesn't exist");
+      validateConfigFiles(log, variables, environment);

Review Comment:
   When the project doesn't exist, `validateConfigFiles()` still runs without 
project variables, so a path like `'${PROJECT_HOME}/dev.json'` creates a file 
in a directory literally named `${PROJECT_HOME}` under the working directory. 
Maybe skip creating the files when the project is missing?



##########
engine/src/main/java/org/apache/hop/config/HopConfigBase.java:
##########
@@ -79,16 +79,41 @@ public void run() {
 
   private boolean handleConfigMixinsOptions(LogChannel log) throws 
HopException {
     boolean actionTaken = false;
+    CommandLine.ParseResult parseResult = cmd.getParseResult();
+    Map<String, CommandLine.Model.CommandSpec> mixinSpecs = 
cmd.getCommandSpec().mixins();
     Map<String, Object> mixins = cmd.getMixins();
-    for (String key : mixins.keySet()) {
-      Object mixin = mixins.get(key);
-      if (mixin instanceof IConfigOptions configOptions) {
+    for (Map.Entry<String, Object> entry : mixins.entrySet()) {
+      // Only hand a plugin its options when at least one of them was given. 
Plugins that load
+      // their current settings when they are created would otherwise save 
them again, and report
+      // a change, on every unrelated hop-conf command.
+      //
+      if (entry.getValue() instanceof IConfigOptions configOptions
+          && hasMatchedOption(parseResult, mixinSpecs.get(entry.getKey()))) {
         actionTaken = configOptions.handleOption(log, this, variables) || 
actionTaken;
       }
     }
     return actionTaken;
   }
 
+  /**
+   * @param parseResult the parsed command line, null if it isn't known
+   * @param mixinSpec the specification of a config plugin mixed into the 
command
+   * @return true if any option of the mixin was given on the command line, or 
if that can't be
+   *     determined
+   */
+  static boolean hasMatchedOption(
+      CommandLine.ParseResult parseResult, CommandLine.Model.CommandSpec 
mixinSpec) {
+    if (parseResult == null || mixinSpec == null) {

Review Comment:
   `hasMatchedOption()` only looks at the mixin's `options()`. A config plugin 
that uses `@CommandLine.Parameters` (positional values) would never get 
`handleOption()` called with this filter. Not an issue for current plugins as 
far as I can see, but checking `positionalParameters()` too would make it 
future-proof.



##########
plugins/misc/projects/src/main/java/org/apache/hop/projects/environment/ManageEnvironmentsOptionPlugin.java:
##########
@@ -243,24 +243,36 @@ private void createEnvironment(
             + HopConfig.getInstance().getConfigFilename());
 
     enableProject(log, environment, variables, hasHopMetadataProvider, config);
-
-    validateConfigFiles(log, variables, environment);
     logEnvironmentDetails(log, environment);
   }
 
+  /**
+   * Create the environment's missing configuration files, then enable its 
project with them.
+   *
+   * <p>The project variables are applied before the files are created, since 
a configuration file
+   * path can refer to them (for example '${PROJECT_HOME}'). Enabling the 
project reads the files,
+   * so they have to exist by then.
+   */
   private void enableProject(
       ILogChannel log,
       LifecycleEnvironment environment,
       IVariables variables,
       IHasHopMetadataProvider hasHopMetadataProvider,
       ProjectsConfig config)
-      throws HopException {
+      throws Exception {
     ProjectConfig projectConfig = 
config.findProjectConfig(environment.getProjectName());
     if (projectConfig == null) {
       log.logBasic(
           "Warning: referenced project '" + environment.getProjectName() + "' 
doesn't exist");
+      validateConfigFiles(log, variables, environment);
     } else {
       Project project = projectConfig.loadProject(variables);
+      // Project variables only, so '${PROJECT_HOME}' in the configuration 
file paths resolves.
+      // enableProject() below applies them again, together with the 
configuration files.
+      //
+      project.modifyVariables(variables, projectConfig, new ArrayList<>(), 
environmentName);

Review Comment:
   `ProjectsUtil.enableProject()` calls `modifyVariables()` again right after 
this, so the parent chain is verified and every parent project is loaded twice. 
With a broken parent project-config.json, one `hop conf -ec` logs "Error 
loading configuration file of parent project ..." twice.



##########
plugins/misc/projects/src/main/java/org/apache/hop/projects/project/ManageProjectsOptionPlugin.java:
##########
@@ -387,39 +387,54 @@ private void createProject(ILogChannel log, 
ProjectsConfig config, IVariables va
         new ProjectConfig(
             projectName, projectHome, Const.NVL(projectConfigFile, 
defaultProjectConfigFilename));
 
-    config.addProjectConfig(projectConfig);
-    ProjectsConfigSingleton.saveConfig();
-
-    log.logBasic(CONST_PROJECT + projectName + "' was created for home folder 
: " + projectHome);
-
+    // This fails when the home folder doesn't exist: do it before the project 
is registered.
+    //
     String configFilename = 
projectConfig.getActualProjectConfigFilename(variables);
     boolean configFileExisted = false;
     try (FileObject configFile = HopVfs.getFileObject(configFilename)) {
       configFileExisted = configFile.exists();
     }
-    Project project = projectConfig.loadProject(variables);
-    // Keep an existing parent from a pre-existing project-config.json (Docker
-    // --project-keep-config-file). Only fall back to the standard parent when 
none is set.
-    // --project-parent still wins via modifyProjectSettings below.
-    //
-    if (StringUtils.isEmpty(project.getParentProjectName())) {
-      
project.setParentProjectName(config.findRegisteredStandardParentProject());
-    }
-    // A brand-new config file starts from the project name. An existing file 
keeps its id,
-    // including none, so a 2.19.0 project is not switched on to execution 
filtering.
-    if (!configFileExisted && StringUtils.isEmpty(project.getProjectId())) {
-      project.setProjectId(projectName);
-    }
-    modifyProjectSettings(project);
 
-    // Check to see if there's not a loop in the project parent hierarchy
-    //
-    project.verifyProjectsChain(projectName, variables);
+    config.addProjectConfig(projectConfig);

Review Comment:
   The rollback only covers failures after 
`ProjectsConfigSingleton.saveConfig()`. If the save itself throws 
(hop-config.json not writable), the project has already been added to the 
in-memory config and is never removed, so a later save in the same process 
persists a project with no config file.



##########
plugins/misc/lint/src/main/java/org/apache/hop/lint/LinterConfigPlugin.java:
##########
@@ -420,11 +420,8 @@ public LinterConfigPlugin() {
   }
 
   /**
-   * Called by the configuration perspective when the user applies changes, 
and by {@code hop conf}
-   * for every registered config plugin whether or not it was given any 
options.
-   *
-   * <p>Because {@code hop conf} calls this unconditionally, doing anything 
when nothing was set
-   * would rewrite the linter settings on every unrelated {@code hop conf} 
command.
+   * Called by {@code hop conf} when at least one of the linter options was 
given. The fields hold

Review Comment:
   Small one: the new Javadoc says `handleOption` is only called by hop conf, 
but the configuration perspective also calls it when the user applies changes. 
Could we keep that note?



##########
ui/src/test/java/org/apache/hop/ui/hopgui/search/config/SearchConfigPluginTest.java:
##########
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hop.ui.hopgui.search.config;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.apache.hop.core.config.HopConfig;
+import org.apache.hop.core.logging.HopLogStore;
+import org.apache.hop.core.logging.LogChannel;
+import org.apache.hop.core.variables.Variables;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import picocli.CommandLine;
+
+/** The search checkboxes are only changed by hop-conf when their option is 
given (#9007). */
+class SearchConfigPluginTest {
+
+  @BeforeAll
+  static void beforeAll() {
+    HopLogStore.init();
+  }
+
+  @BeforeEach
+  void inMemory() {
+    HopConfig.setInMemoryMode(true);
+  }
+
+  @AfterEach
+  void restore() {
+    SearchConfig config = SearchConfigSingleton.getConfig();
+    config.setSearchAsYouType(null);

Review Comment:
   The teardown resets the shared `SearchConfig` singleton to null instead of 
restoring the values it had before the test 
(`ExplorerPerspectiveConfigPluginTest` does capture and restore them). Another 
test in the same surefire JVM could get its settings wiped, making results 
order-dependent.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to