This is an automated email from the ASF dual-hosted git repository. rmaucher pushed a commit to branch 11.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 181732d79128d8dc661b3af19b5b803ce6cc8509 Author: opencode <[email protected]> AuthorDate: Sun Oct 18 21:19:40 2026 +0200 Report compilation errors before removing the generated source The JSP compiler backends deleted the generated servlet source when keepGenerated was false before dispatching the accumulated compiler diagnostics to the error handler. If the delete failed, the exception thrown for it masked the compilation errors that were the primary problem, leaving the user without any indication of why the JSP failed to compile. Dispatch the errors first, then remove the generated source. As a side effect, the generated source of a failed compilation is now retained for inspection, since dispatching the errors completes abruptly. This is safe: a subsequent compilation attempt always removes any retained generated source before regenerating it. Add TestJavaCompiler to verify that compilation errors are reported and the generated source is retained after a failed compilation. --- java/org/apache/jasper/compiler/AntCompiler.java | 16 +-- java/org/apache/jasper/compiler/JDTCompiler.java | 12 ++- java/org/apache/jasper/compiler/JavaCompiler.java | 12 ++- .../apache/jasper/compiler/TestJavaCompiler.java | 114 +++++++++++++++++++++ 4 files changed, 137 insertions(+), 17 deletions(-) diff --git a/java/org/apache/jasper/compiler/AntCompiler.java b/java/org/apache/jasper/compiler/AntCompiler.java index a00f9f6ed2..2ba27feef5 100644 --- a/java/org/apache/jasper/compiler/AntCompiler.java +++ b/java/org/apache/jasper/compiler/AntCompiler.java @@ -260,13 +260,8 @@ public class AntCompiler extends Compiler { errorReport.append(errorCapture); } - if (!ctxt.keepGenerated()) { - File javaFile = new File(javaFileName); - if (!javaFile.delete()) { - throw new JasperException(Localizer.getMessage("jsp.warning.compiler.javafile.delete.fail", javaFile)); - } - } - + // Dispatch any compilation errors before deleting the generated + // source, so a failure to delete can not mask the real problem if (be != null) { String errorReportString = errorReport.toString(); log.error(Localizer.getMessage("jsp.error.compilation", javaFileName, errorReportString)); @@ -279,6 +274,13 @@ public class AntCompiler extends Compiler { } } + if (!ctxt.keepGenerated()) { + File javaFile = new File(javaFileName); + if (!javaFile.delete()) { + throw new JasperException(Localizer.getMessage("jsp.warning.compiler.javafile.delete.fail", javaFile)); + } + } + if (log.isDebugEnabled()) { long t2 = System.currentTimeMillis(); log.debug(Localizer.getMessage("jsp.compiled", ctxt.getServletJavaFileName(), Long.valueOf(t2 - t1))); diff --git a/java/org/apache/jasper/compiler/JDTCompiler.java b/java/org/apache/jasper/compiler/JDTCompiler.java index c9fe56aa7f..1c9960e002 100644 --- a/java/org/apache/jasper/compiler/JDTCompiler.java +++ b/java/org/apache/jasper/compiler/JDTCompiler.java @@ -539,6 +539,13 @@ public class JDTCompiler extends org.apache.jasper.compiler.Compiler { Compiler compiler = new Compiler(env, policy, cOptions, requestor, problemFactory); compiler.compile(compilationUnits); + // Dispatch any compilation problems before deleting the generated + // source, so a failure to delete can not mask the real problem + if (!problemList.isEmpty()) { + JavacErrorDetail[] jeds = problemList.toArray(new JavacErrorDetail[0]); + errDispatcher.javacError(jeds); + } + if (!ctxt.keepGenerated()) { File javaFile = new File(ctxt.getServletJavaFileName()); if (!javaFile.delete()) { @@ -546,11 +553,6 @@ public class JDTCompiler extends org.apache.jasper.compiler.Compiler { } } - if (!problemList.isEmpty()) { - JavacErrorDetail[] jeds = problemList.toArray(new JavacErrorDetail[0]); - errDispatcher.javacError(jeds); - } - if (log.isDebugEnabled()) { long t2 = System.currentTimeMillis(); log.debug(Localizer.getMessage("jsp.compiled", ctxt.getServletJavaFileName(), Long.valueOf(t2 - t1))); diff --git a/java/org/apache/jasper/compiler/JavaCompiler.java b/java/org/apache/jasper/compiler/JavaCompiler.java index 86443ac761..c9ef655fc5 100644 --- a/java/org/apache/jasper/compiler/JavaCompiler.java +++ b/java/org/apache/jasper/compiler/JavaCompiler.java @@ -83,6 +83,13 @@ public class JavaCompiler extends Compiler { } } + // Dispatch any compilation errors before deleting the generated + // source, so a failure to delete can not mask the real problem + if (!problemList.isEmpty()) { + JavacErrorDetail[] jeds = problemList.toArray(new JavacErrorDetail[0]); + errDispatcher.javacError(jeds); + } + if (!ctxt.keepGenerated()) { File javaFile = new File(ctxt.getServletJavaFileName()); if (!javaFile.delete()) { @@ -90,11 +97,6 @@ public class JavaCompiler extends Compiler { } } - if (!problemList.isEmpty()) { - JavacErrorDetail[] jeds = problemList.toArray(new JavacErrorDetail[0]); - errDispatcher.javacError(jeds); - } - if (log.isDebugEnabled()) { long t2 = System.currentTimeMillis(); log.debug(Localizer.getMessage("jsp.compiled", ctxt.getServletJavaFileName(), Long.valueOf(t2 - t1))); diff --git a/test/org/apache/jasper/compiler/TestJavaCompiler.java b/test/org/apache/jasper/compiler/TestJavaCompiler.java new file mode 100644 index 0000000000..452a37322d --- /dev/null +++ b/test/org/apache/jasper/compiler/TestJavaCompiler.java @@ -0,0 +1,114 @@ +/* + * 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.jasper.compiler; + +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import org.apache.jasper.JasperException; +import org.apache.jasper.JspCompilationContext; +import org.apache.jasper.Options; +import org.easymock.EasyMock; + +public class TestJavaCompiler { + + private File outputDir; + private File javaFile; + + @Before + public void setUp() throws Exception { + File tempDir = new File(System.getProperty("tomcat.test.temp", + "output/tmp")); + outputDir = new File(tempDir, "java-compiler"); + Assert.assertTrue(outputDir.mkdirs() || outputDir.isDirectory()); + javaFile = new File(outputDir, "Bad_jsp.java"); + String source = "public class Bad_jsp {\n" + + " public void service() {\n" + + " int i = ;\n" + + " }\n" + + "}\n"; + Files.write(javaFile.toPath(), + source.getBytes(StandardCharsets.UTF_8)); + } + + @After + public void tearDown() { + removeFile(javaFile); + removeFile(new File(outputDir, "Bad_jsp.class")); + outputDir.delete(); + } + + private static void removeFile(File file) { + if (file != null && file.exists()) { + file.delete(); + } + } + + /* + * The compilation errors must be dispatched to the error handler before + * the generated source is removed, so that the errors are reported (and + * the source retained for inspection) rather than being masked by the + * cleanup of the generated source. + */ + @Test + public void testCompilationErrorsDispatchedBeforeGeneratedSourceRemoved() + throws Exception { + Options options = EasyMock.createMock(Options.class); + EasyMock.expect(options.getJavaEncoding()).andReturn("UTF-8") + .anyTimes(); + EasyMock.expect(options.getCompilerSourceVM()).andReturn("17") + .anyTimes(); + EasyMock.expect(options.getCompilerTargetVM()).andReturn("17") + .anyTimes(); + EasyMock.replay(options); + + JspCompilationContext ctxt = EasyMock.createMock( + JspCompilationContext.class); + EasyMock.expect(ctxt.getOptions()).andReturn(options).anyTimes(); + EasyMock.expect(ctxt.getServletJavaFileName()) + .andReturn(javaFile.getAbsolutePath()).anyTimes(); + EasyMock.expect(ctxt.getClassPath()) + .andReturn(System.getProperty("java.class.path")).anyTimes(); + EasyMock.expect(ctxt.keepGenerated()).andReturn(false).anyTimes(); + EasyMock.replay(ctxt); + + JavaCompiler compiler = new JavaCompiler(); + compiler.init(ctxt, null); + compiler.pageNodes = new Node.Nodes(); + compiler.errDispatcher = new ErrorDispatcher(false); + + try { + compiler.generateClass(null); + Assert.fail("Compilation was expected to fail"); + } catch (JasperException e) { + // The javac error must have been reported ... + Assert.assertTrue("Unexpected message: " + e.getMessage(), + e.getMessage().contains("Bad_jsp.java")); + // ... before the generated source was removed, which also means + // the source is retained for inspection after a failure + Assert.assertTrue(javaFile.exists()); + } + + EasyMock.verify(options, ctxt); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
