Copilot commented on code in PR #12629:
URL: https://github.com/apache/maven/pull/12629#discussion_r3682915920


##########
impl/maven-core/src/main/java/org/apache/maven/DefaultMaven.java:
##########
@@ -210,20 +210,22 @@ private MavenExecutionResult 
doExecute(MavenExecutionRequest request) {
         // so that @SessionScoped components can be @Injected into 
AbstractLifecycleParticipants.
         //
         sessionScope.enter();
-        MavenChainedWorkspaceReader chainedWorkspaceReader =
-                new MavenChainedWorkspaceReader(request.getWorkspaceReader(), 
ideWorkspaceReader);
-        try (CloseableSession closeableSession = newCloseableSession(request, 
chainedWorkspaceReader)) {
-            MavenSession session = new MavenSession(closeableSession, request, 
result);
-            session.setSession(defaultSessionFactory.newSession(session));
+        try {
+            MavenChainedWorkspaceReader chainedWorkspaceReader =

Review Comment:
   The PR description says `sessionScope.enter()` is wrapped in the new outer 
`try/finally`, but in the diff it remains outside. This is likely fine (avoids 
calling `exit()` if `enter()` itself fails), but the description should be 
updated to match the implementation to prevent confusion during future audits.



##########
impl/maven-core/src/test/java/org/apache/maven/DefaultMavenSessionScopeTest.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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.maven;
+
+import java.io.File;
+import java.lang.reflect.Field;
+import java.nio.file.Files;
+import java.util.List;
+
+import org.apache.maven.api.services.Lookup;
+import org.apache.maven.execution.BuildResumptionAnalyzer;
+import org.apache.maven.execution.BuildResumptionDataRepository;
+import org.apache.maven.execution.DefaultMavenExecutionRequest;
+import org.apache.maven.execution.MavenExecutionRequest;
+import org.apache.maven.execution.MavenExecutionResult;
+import org.apache.maven.graph.GraphBuilder;
+import org.apache.maven.internal.impl.DefaultSessionFactory;
+import org.apache.maven.lifecycle.internal.ExecutionEventCatapult;
+import org.apache.maven.plugin.LegacySupport;
+import org.apache.maven.resolver.RepositorySystemSessionFactory;
+import org.apache.maven.session.scope.internal.SessionScope;
+import org.eclipse.aether.repository.WorkspaceReader;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class DefaultMavenSessionScopeTest {
+
+    @Test
+    void testSessionScopeIsExitedOnWorkspaceReaderError() throws Exception {
+        WorkspaceReader badReader = mock(WorkspaceReader.class);
+        when(badReader.getRepository()).thenReturn(null);
+
+        File localRepoDir = Files.createTempDirectory("local-repo").toFile();
+        localRepoDir.deleteOnExit();
+        MavenExecutionRequest request = new DefaultMavenExecutionRequest()

Review Comment:
   Using `deleteOnExit()` for temp directories can accumulate disk usage during 
long-running test suites (cleanup only happens when the JVM exits, and contents 
may not be removed). Prefer JUnit 5’s `@TempDir` to ensure deterministic 
cleanup after the test completes.



##########
impl/maven-core/src/test/java/org/apache/maven/DefaultMavenSessionScopeTest.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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.maven;
+
+import java.io.File;
+import java.lang.reflect.Field;
+import java.nio.file.Files;
+import java.util.List;
+
+import org.apache.maven.api.services.Lookup;
+import org.apache.maven.execution.BuildResumptionAnalyzer;
+import org.apache.maven.execution.BuildResumptionDataRepository;
+import org.apache.maven.execution.DefaultMavenExecutionRequest;
+import org.apache.maven.execution.MavenExecutionRequest;
+import org.apache.maven.execution.MavenExecutionResult;
+import org.apache.maven.graph.GraphBuilder;
+import org.apache.maven.internal.impl.DefaultSessionFactory;
+import org.apache.maven.lifecycle.internal.ExecutionEventCatapult;
+import org.apache.maven.plugin.LegacySupport;
+import org.apache.maven.resolver.RepositorySystemSessionFactory;
+import org.apache.maven.session.scope.internal.SessionScope;
+import org.eclipse.aether.repository.WorkspaceReader;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class DefaultMavenSessionScopeTest {
+
+    @Test
+    void testSessionScopeIsExitedOnWorkspaceReaderError() throws Exception {
+        WorkspaceReader badReader = mock(WorkspaceReader.class);
+        when(badReader.getRepository()).thenReturn(null);
+
+        File localRepoDir = Files.createTempDirectory("local-repo").toFile();
+        localRepoDir.deleteOnExit();
+        MavenExecutionRequest request = new DefaultMavenExecutionRequest()
+                .setLocalRepositoryPath(localRepoDir)
+                .setWorkspaceReader(badReader);
+
+        SessionScope sessionScope = new SessionScope();
+
+        DefaultMaven defaultMaven = new DefaultMaven(
+                mock(Lookup.class),
+                mock(ExecutionEventCatapult.class),
+                mock(LegacySupport.class),
+                sessionScope,
+                mock(RepositorySystemSessionFactory.class),
+                mock(GraphBuilder.class),
+                mock(BuildResumptionAnalyzer.class),
+                mock(BuildResumptionDataRepository.class),
+                null,
+                mock(DefaultSessionFactory.class),
+                null);
+
+        MavenExecutionResult result = defaultMaven.execute(request);
+
+        assertFalse(result.getExceptions().isEmpty());
+
+        Field valuesField = 
SessionScope.class.getSuperclass().getDeclaredField("values");
+        valuesField.setAccessible(true);
+        List<?> values = (List<?>) valuesField.get(sessionScope);
+        assertTrue(values.isEmpty(), "Session scope must be exited even on 
workspace reader error");

Review Comment:
   The test relies on reflective access to a private `values` field on 
`SessionScope`'s superclass. This is brittle (field renames, superclass 
changes) and can fail under stronger encapsulation (e.g., module access 
restrictions). Prefer asserting cleanup via a supported surface (e.g., an 
explicit package-private test hook/accessor on `SessionScope`, or an observable 
behavioral check such as re-enter/exit invariants if available) instead of 
reflecting into internals.



##########
impl/maven-core/src/test/java/org/apache/maven/DefaultMavenSessionScopeTest.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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.maven;
+
+import java.io.File;
+import java.lang.reflect.Field;
+import java.nio.file.Files;
+import java.util.List;
+
+import org.apache.maven.api.services.Lookup;
+import org.apache.maven.execution.BuildResumptionAnalyzer;
+import org.apache.maven.execution.BuildResumptionDataRepository;
+import org.apache.maven.execution.DefaultMavenExecutionRequest;
+import org.apache.maven.execution.MavenExecutionRequest;
+import org.apache.maven.execution.MavenExecutionResult;
+import org.apache.maven.graph.GraphBuilder;
+import org.apache.maven.internal.impl.DefaultSessionFactory;
+import org.apache.maven.lifecycle.internal.ExecutionEventCatapult;
+import org.apache.maven.plugin.LegacySupport;
+import org.apache.maven.resolver.RepositorySystemSessionFactory;
+import org.apache.maven.session.scope.internal.SessionScope;
+import org.eclipse.aether.repository.WorkspaceReader;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class DefaultMavenSessionScopeTest {
+
+    @Test
+    void testSessionScopeIsExitedOnWorkspaceReaderError() throws Exception {
+        WorkspaceReader badReader = mock(WorkspaceReader.class);
+        when(badReader.getRepository()).thenReturn(null);
+
+        File localRepoDir = Files.createTempDirectory("local-repo").toFile();
+        localRepoDir.deleteOnExit();
+        MavenExecutionRequest request = new DefaultMavenExecutionRequest()
+                .setLocalRepositoryPath(localRepoDir)
+                .setWorkspaceReader(badReader);
+
+        SessionScope sessionScope = new SessionScope();
+
+        DefaultMaven defaultMaven = new DefaultMaven(
+                mock(Lookup.class),
+                mock(ExecutionEventCatapult.class),
+                mock(LegacySupport.class),
+                sessionScope,
+                mock(RepositorySystemSessionFactory.class),
+                mock(GraphBuilder.class),
+                mock(BuildResumptionAnalyzer.class),
+                mock(BuildResumptionDataRepository.class),
+                null,
+                mock(DefaultSessionFactory.class),
+                null);
+
+        MavenExecutionResult result = defaultMaven.execute(request);
+
+        assertFalse(result.getExceptions().isEmpty());

Review Comment:
   The assertion only checks that *some* exception happened. This can allow 
false positives if a different failure occurs earlier than the intended 
`MavenChainedWorkspaceReader` null-repo path. Consider asserting that the 
exception is (or is caused by) the expected `NullPointerException`/originating 
class to ensure the regression test is validating the intended scenario.



-- 
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