@@ -247,6 +247,128 @@ TEST(c_trailing_recovered_defs_keep_flag) {
247247
248248/* ── Suite ────────────────────────────────────────────────────────────────── */
249249
250+ /* ── #1610: a missing FINAL NEWLINE is not a parse failure ────────────────────
251+ *
252+ * A file that does not end with "\n" leaves the grammar's mandatory line
253+ * terminator MISSING. That node is ZERO-WIDTH and sits at EOF: the parser
254+ * consumed no source for it, so by construction nothing was dropped — no
255+ * construct can live in a zero-byte span. Every instruction still parses.
256+ *
257+ * Reported on #1610 for Dockerfile, where a reporter proved with a byte-exact
258+ * matrix that the trigger is independent of BOM, CRLF/LF, exec-form vs
259+ * shell-form and file length — it is purely the absent final newline.
260+ *
261+ * It was never Dockerfile-specific: tcl, fish, gomod and hyprlang flag the same
262+ * way, while ini, fsharp, beancount and others do NOT — only because those
263+ * grammars declare the terminator token hidden rather than visible. Whether a
264+ * user saw a phantom parse_partial came down to a grammar-authoring accident.
265+ *
266+ * The cost was not cosmetic: a phantom flag writes a "<project>::missed" shadow
267+ * row, and until #1609 that row removed the whole project from cross-repo
268+ * linking, as source AND as target. */
269+ TEST (dockerfile_missing_final_newline_not_flagged_issue1610 ) {
270+ const char * src = "FROM mcr.microsoft.com/dotnet/aspnet:8.0\n"
271+ "ENTRYPOINT [\"dotnet\", \"App.dll\"]" ; /* deliberately no \n */
272+ CBMFileResult * r = do_extract (src , CBM_LANG_DOCKERFILE , "Dockerfile" );
273+ ASSERT_NOT_NULL (r );
274+ bool flagged = r -> parse_incomplete ;
275+ cbm_free_result (r );
276+ if (flagged ) {
277+ FAIL ("a Dockerfile lacking only its final newline must not be parse_partial" );
278+ }
279+ PASS ();
280+ }
281+
282+ /* The same bytes WITH the newline must stay clean — pins the equivalence the
283+ * reporter's matrix proved, so a future change cannot "fix" one by breaking the
284+ * other. */
285+ TEST (dockerfile_with_final_newline_still_clean_issue1610 ) {
286+ const char * src = "FROM mcr.microsoft.com/dotnet/aspnet:8.0\n"
287+ "ENTRYPOINT [\"dotnet\", \"App.dll\"]\n" ;
288+ CBMFileResult * r = do_extract (src , CBM_LANG_DOCKERFILE , "Dockerfile" );
289+ ASSERT_NOT_NULL (r );
290+ bool flagged = r -> parse_incomplete ;
291+ cbm_free_result (r );
292+ if (flagged ) {
293+ FAIL ("a terminated Dockerfile must not be parse_partial" );
294+ }
295+ PASS ();
296+ }
297+
298+ /* Language-general, not a Dockerfile patch: these four were each proven to flag
299+ * on a stripped trailing newline. */
300+ TEST (missing_final_newline_not_flagged_across_grammars_issue1610 ) {
301+ struct {
302+ const char * src ;
303+ CBMLanguage lang ;
304+ const char * path ;
305+ } cases [] = {
306+ {"proc foo {} {}\nproc bar {} {}" , CBM_LANG_TCL , "a.tcl" },
307+ {"function foo\n echo hi\nend" , CBM_LANG_FISH , "a.fish" },
308+ {"module example.com/m\n\ngo 1.21" , CBM_LANG_GOMOD , "go.mod" },
309+ {"general {\n gaps_in = 5\n}" , CBM_LANG_HYPRLANG , "hypr.conf" },
310+ };
311+ for (size_t i = 0 ; i < sizeof (cases ) / sizeof (cases [0 ]); i ++ ) {
312+ CBMFileResult * r = do_extract (cases [i ].src , cases [i ].lang , cases [i ].path );
313+ ASSERT_NOT_NULL (r );
314+ bool flagged = r -> parse_incomplete ;
315+ if (flagged ) {
316+ fprintf (stderr , " %s flagged: ranges=%s\n" , cases [i ].path ,
317+ r -> error_ranges ? r -> error_ranges : "(none)" );
318+ }
319+ cbm_free_result (r );
320+ if (flagged ) {
321+ FAIL ("an unterminated final line must not be parse_partial in any grammar" );
322+ }
323+ }
324+ PASS ();
325+ }
326+
327+ /* GUARD (the reason this suppression is safe rather than convenient): the rule
328+ * is ZERO-WIDTH AT EOF only. A real failure earlier in the file must still be
329+ * reported, and its range must name the broken line — not be swallowed along
330+ * with the terminator. */
331+ TEST (real_error_before_eof_still_flagged_without_final_newline_issue1610 ) {
332+ /* Built from C_IFDEF_SPLIT, the fixture this suite already proves is
333+ * flagged, with its trailing newline removed. Two conditions now hold at
334+ * once: a genuine width-bearing ERROR mid-file, AND an unterminated last
335+ * line. Suppressing the EOF terminator must not swallow the real one. */
336+ size_t n = strlen (C_IFDEF_SPLIT );
337+ char * unterminated = (char * )malloc (n + 1 );
338+ ASSERT_NOT_NULL (unterminated );
339+ memcpy (unterminated , C_IFDEF_SPLIT , n );
340+ unterminated [n - 1 ] = '\0' ; /* drop the final newline */
341+
342+ CBMFileResult * r = do_extract (unterminated , CBM_LANG_C , "split.c" );
343+ free (unterminated );
344+ ASSERT_NOT_NULL (r );
345+ bool flagged = r -> parse_incomplete ;
346+ bool has_ranges = r -> error_ranges != NULL ;
347+ cbm_free_result (r );
348+ if (!flagged ) {
349+ FAIL ("a real mid-file parse failure must still be reported when the file also lacks its final newline" );
350+ }
351+ if (!has_ranges ) {
352+ FAIL ("a reported failure must still name its line range" );
353+ }
354+ PASS ();
355+ }
356+
357+ /* GUARD: a MISSING/ERROR node WITH WIDTH at EOF is a genuine loss and must
358+ * still be flagged. A Makefile whose final recipe line lacks its newline really
359+ * does drop the recipe from the tree — cbm's flag is honest there. */
360+ TEST (width_bearing_error_at_eof_still_flagged_issue1610 ) {
361+ const char * src = "all:\n\techo hi" ; /* no trailing newline; recipe is lost */
362+ CBMFileResult * r = do_extract (src , CBM_LANG_MAKEFILE , "Makefile" );
363+ ASSERT_NOT_NULL (r );
364+ bool flagged = r -> parse_incomplete ;
365+ cbm_free_result (r );
366+ if (!flagged ) {
367+ FAIL ("a width-bearing parse failure at EOF must still be reported" );
368+ }
369+ PASS ();
370+ }
371+
250372SUITE (parse_coverage ) {
251373 RUN_TEST (c_ifdef_split_brace_sets_parse_incomplete );
252374 RUN_TEST (c_ifdef_split_brace_neighbors_still_extracted );
@@ -257,4 +379,9 @@ SUITE(parse_coverage) {
257379 RUN_TEST (py_clean_file_not_flagged );
258380 RUN_TEST (error_region_cap_is_honored );
259381 RUN_TEST (c_trailing_recovered_defs_keep_flag );
382+ RUN_TEST (dockerfile_missing_final_newline_not_flagged_issue1610 );
383+ RUN_TEST (dockerfile_with_final_newline_still_clean_issue1610 );
384+ RUN_TEST (missing_final_newline_not_flagged_across_grammars_issue1610 );
385+ RUN_TEST (real_error_before_eof_still_flagged_without_final_newline_issue1610 );
386+ RUN_TEST (width_bearing_error_at_eof_still_flagged_issue1610 );
260387}
0 commit comments