From 217689524af8e61c03adab9d29af0404750dc23a Mon Sep 17 00:00:00 2001 From: sergioperezcheco Date: Thu, 8 Oct 2026 21:47:57 +0800 Subject: [PATCH 1/2] Fix CLI template names with trailing root separators Assisted-by: Hermes Agent Signed-off-by: sergioperezcheco --- lib/precompiler.js | 8 ++++++-- spec/precompiler.js | 47 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/lib/precompiler.js b/lib/precompiler.js index a8e69d6c..fc6e3077 100644 --- a/lib/precompiler.js +++ b/lib/precompiler.js @@ -126,8 +126,12 @@ function loadFiles(opts, callback) { let name = path; if (!root) { name = basename(name); - } else if (name.indexOf(root) === 0) { - name = name.substring(root.length + 1); + } else if ( + name.indexOf(root.endsWith('/') ? root : root + '/') === 0 + ) { + name = name.substring( + root.length + (name.charAt(root.length) === '/' ? 1 : 0) + ); } name = name.replace(extension, ''); diff --git a/spec/precompiler.js b/spec/precompiler.js index 3c4d3b8f..83d6af24 100644 --- a/spec/precompiler.js +++ b/spec/precompiler.js @@ -252,6 +252,53 @@ describe('precompiler', function () { expect(opts.templates[0].source).toBe('a'); }); + it('should preserve template names with a trailing root separator', async function () { + for (const root of [ + __dirname + '/artifacts', + __dirname + '/artifacts/', + ]) { + var opts = await loadTemplatesAsync({ + files: [__dirname + '/artifacts/empty.handlebars'], + root: root, + }); + expect(opts.templates[0].name).toBe('empty'); + } + }); + + it('should preserve nested template names with a trailing root separator', async function () { + for (const root of [__dirname, __dirname + '/']) { + var opts = await loadTemplatesAsync({ + files: [__dirname + '/artifacts/empty.handlebars'], + root: root, + }); + expect(opts.templates[0].name).toBe('artifacts/empty'); + } + }); + + it('should enumerate directories with a trailing root separator', async function () { + for (const directory of [ + __dirname + '/artifacts', + __dirname + '/artifacts/', + ]) { + var opts = await loadTemplatesAsync({ + files: [directory], + extension: 'hbs', + }); + expect(opts.templates.map((template) => template.name)).toEqual([ + 'example_2', + 'non.default.extension', + ]); + } + }); + + it('should not strip a root that only matches part of a directory name', async function () { + var opts = await loadTemplatesAsync({ + files: [__dirname + '/artifacts/empty.handlebars'], + root: __dirname + '/art', + }); + expect(opts.templates[0].name).toBe(__dirname + '/artifacts/empty'); + }); + it('should handle different root', async function () { var opts = await loadTemplatesAsync({ files: [__dirname + '/artifacts/empty.handlebars'], From a95ebbfcecd225bc4fd21983550b3fa90ab92b56 Mon Sep 17 00:00:00 2001 From: sergioperezcheco Date: Tue, 20 Oct 2026 11:16:34 +0800 Subject: [PATCH 2/2] fix(cli): handle native separators when stripping template root Assisted-by: Hermes Agent (Nous Research) Signed-off-by: sergioperezcheco --- lib/precompiler.js | 25 ++++++++++++++++++------- spec/precompiler.js | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/lib/precompiler.js b/lib/precompiler.js index fc6e3077..e694f036 100644 --- a/lib/precompiler.js +++ b/lib/precompiler.js @@ -2,7 +2,7 @@ import Async from 'neo-async'; import fs from 'fs'; import Handlebars from './handlebars.js'; -import { basename } from 'path'; +import { basename, sep } from 'path'; import { SourceMapConsumer, SourceNode } from 'source-map'; export function loadTemplates(opts, callback) { @@ -126,12 +126,23 @@ function loadFiles(opts, callback) { let name = path; if (!root) { name = basename(name); - } else if ( - name.indexOf(root.endsWith('/') ? root : root + '/') === 0 - ) { - name = name.substring( - root.length + (name.charAt(root.length) === '/' ? 1 : 0) - ); + } else { + const normalizedName = name.split(sep).join('/'); + const normalizedRoot = root.split(sep).join('/'); + if ( + normalizedName.indexOf( + normalizedRoot.endsWith('/') + ? normalizedRoot + : normalizedRoot + '/' + ) === 0 + ) { + name = normalizedName.substring( + normalizedRoot.length + + (normalizedName.charAt(normalizedRoot.length) === '/' + ? 1 + : 0) + ); + } } name = name.replace(extension, ''); diff --git a/spec/precompiler.js b/spec/precompiler.js index 83d6af24..6bab0ad5 100644 --- a/spec/precompiler.js +++ b/spec/precompiler.js @@ -252,6 +252,46 @@ describe('precompiler', function () { expect(opts.templates[0].source).toBe('a'); }); + it.skipIf(path.sep !== '\\')( + 'should strip native Windows roots from explicit and enumerated paths', + async function () { + const directory = path.join(__dirname, 'artifacts'); + for (const root of [directory, directory + path.sep]) { + const direct = await loadTemplatesAsync({ + files: [path.join(directory, 'empty.handlebars')], + root, + }); + expect(direct.templates[0].name).toBe('empty'); + const enumerated = await loadTemplatesAsync({ + files: [directory], + root, + }); + expect( + enumerated.templates.map((template) => template.name) + ).toContain('empty'); + } + } + ); + + it.skipIf(path.sep !== '\\')( + 'should normalize native Windows names but preserve root boundaries', + async function () { + const filename = path.join(__dirname, 'artifacts', 'empty.handlebars'); + for (const root of [__dirname, __dirname + path.sep, __dirname + '/']) { + const opts = await loadTemplatesAsync({ files: [filename], root }); + expect(opts.templates[0].name).toBe('artifacts/empty'); + expect(opts.templates[0].path).toBe(filename); + } + const opts = await loadTemplatesAsync({ + files: [filename], + root: path.join(__dirname, 'art'), + }); + expect(opts.templates[0].name).toBe( + filename.replace(/\.handlebars$/, '') + ); + } + ); + it('should preserve template names with a trailing root separator', async function () { for (const root of [ __dirname + '/artifacts',