diff --git a/plugin-tests/dev-tooling/codex_manifest.bats b/plugin-tests/dev-tooling/codex_manifest.bats index a28c009..80e0978 100644 --- a/plugin-tests/dev-tooling/codex_manifest.bats +++ b/plugin-tests/dev-tooling/codex_manifest.bats @@ -203,7 +203,7 @@ _assert_listing_matches_tools_json() { # bats test_tags=directives @test "the Claude directives send a ludtwig vendor/ refusal to php-tooling instead of the server that refused" { # shellcheck disable=SC2016 # literal backticks in the prompt text - run grep -F 'a ludtwig refusal on a missing vendor/ names `php-tooling`' "${DIRECTIVES}" + run grep -F 'A ludtwig refusal on the `js-storefront-tooling` MCP server over a missing vendor/ names `php-tooling`.' "${DIRECTIVES}" assert_success run grep -F 'MCP server that refused' "${DIRECTIVES}" assert_failure diff --git a/plugin-tests/dev-tooling/js_admin_tools.bats b/plugin-tests/dev-tooling/js_admin_tools.bats index 940e040..2e861b2 100644 --- a/plugin-tests/dev-tooling/js_admin_tools.bats +++ b/plugin-tests/dev-tooling/js_admin_tools.bats @@ -64,18 +64,21 @@ bats_test_function --description "blocks npx eslint → suggests eslint_check" \ bats_test_function --description "blocks npx jest → suggests jest_run" \ -- js_admin_hook_blocks "npx jest --watch" "jest_run" -# The target-less base scripts. No MCP tool calls them. Shopware's -# Administration package.json defines lint:debugging only, and the redirects -# stay for a project that defines the others. # bats test_tags=blocking,base-scripts bats_test_function --description "blocks npm run lint:debugging → suggests eslint_check" \ -- js_admin_hook_blocks "npm run lint:debugging" "eslint_check" -bats_test_function --description "blocks npm run stylelint:base → suggests stylelint_check" \ - -- js_admin_hook_blocks "npm run stylelint:base" "stylelint_check" -bats_test_function --description "blocks npm run prettier:base → suggests prettier_check" \ - -- js_admin_hook_blocks "npm run prettier:base" "prettier_check" -bats_test_function --description "blocks npm run jest:base → suggests jest_run" \ - -- js_admin_hook_blocks "npm run jest:base" "jest_run" + +# The Administration package defines `lint-fix` beside `lint:fix`. +# bats test_tags=blocking +bats_test_function --description "blocks npm run lint-fix → suggests eslint_fix" \ + -- js_admin_hook_blocks "npm run lint-fix" "eslint_fix" + +# jest_run's parameter is testPathPatterns; testPathPattern is not one. +# bats test_tags=blocking +bats_test_function --description "the npm run unit block names the testPathPatterns parameter" \ + -- js_admin_hook_blocks "npm run unit" "testPathPatterns" +bats_test_function --description "the jest binary block names the testPathPatterns parameter" \ + -- js_admin_hook_blocks "npx jest src" "testPathPatterns" # A linter binary run through `npx`, `npm exec` or its node_modules/.bin path. # The MCP servers run the binaries themselves through `npm exec --no --`, so an @@ -141,23 +144,54 @@ bats_test_function --description "allows reading a linter binary" \ bats_test_function --description "allows a linter name inside quoted text after a semicolon" \ -- admin_hook_allows 'git commit -m "tidy; eslint config"' -# bats test_tags=context,allow -@test "allows npm run stylelint:app, a Storefront script name" { - run_hook "check-js-admin-tools.sh" "npm run stylelint:app" - assert_success -} - -# bats test_tags=context,allow -@test "allows npm run eslint:app, a Storefront script name" { - run_hook "check-js-admin-tools.sh" "npm run eslint:app" - assert_success +# An env assignment ahead of npm, npm flags around `run`, the `run-script` +# alias, and a `;` glued to the script name still run it. +# bats test_tags=blocking +bats_test_function --description "blocks npm run lint behind an env assignment → suggests eslint_check" \ + -- js_admin_hook_blocks "CI=true npm run lint" "eslint_check" +bats_test_function --description "blocks npm run with a flag before the script name → suggests eslint_check" \ + -- js_admin_hook_blocks "npm run --silent lint" "eslint_check" +bats_test_function --description "blocks npm --prefix run before the script name → suggests tsc_check" \ + -- js_admin_hook_blocks "npm --prefix src/Administration/Resources/app/administration run lint:types" "tsc_check" +bats_test_function --description "blocks npm run-script build → suggests vite_build" \ + -- js_admin_hook_blocks "npm run-script build" "vite_build" +bats_test_function --description "blocks npm run lint glued to ; → suggests eslint_check" \ + -- js_admin_hook_blocks "npm run lint;echo done" "eslint_check" + +# Each npm-script row matches its own script name only. +# bats test_tags=blocking +@test "npm run lint:fix glued to ; is routed to eslint_fix, not eslint_check" { + run_hook "check-js-admin-tools.sh" "npm run lint:fix;echo done" + assert_failure 2 + assert_output --partial "eslint_fix" + refute_output --partial "eslint_check" } -# bats test_tags=context,allow -@test "allows npm run jest:base when the command names the Storefront tree" { - run_hook "check-js-admin-tools.sh" "cd src/Storefront/Resources/app/storefront && npm run jest:base" - assert_success -} +# bats test_tags=allow +bats_test_function --description "allows npm run lint--x, a longer script name" \ + -- admin_hook_allows "npm run lint--x" +bats_test_function --description "allows npm run lintx, a longer script name" \ + -- admin_hook_allows "npm run lintx" +bats_test_function --description "allows npm run build--watch, a longer script name" \ + -- admin_hook_allows "npm run build--watch" +bats_test_function --description "allows an env-prefixed npm run named inside a quoted commit message" \ + -- admin_hook_allows 'git commit -m "use CI=true npm run lint"' + +# A binary run with `;` glued to its name, behind an env assignment, or through +# node and its node_modules/.bin path. +# bats test_tags=blocking,binaries +bats_test_function --description "blocks npx eslint glued to ; → suggests eslint_check" \ + -- js_admin_hook_blocks "npx eslint;" "eslint_check" +bats_test_function --description "blocks npx eslint behind an env assignment → suggests eslint_check" \ + -- js_admin_hook_blocks "CI=true npx eslint ." "eslint_check" +bats_test_function --description "blocks node node_modules/.bin/eslint → suggests eslint_check" \ + -- js_admin_hook_blocks "node node_modules/.bin/eslint src" "eslint_check" + +# The anchor has no `(`, so a binary inside parentheses in a quoted commit +# message is not a run. +# bats test_tags=binaries,allow +bats_test_function --description "allows npx eslint inside parentheses in a quoted commit message" \ + -- admin_hook_allows 'git commit -m "fix (npx eslint)"' # bats test_tags=allow @test "allows unrelated commands" { @@ -182,8 +216,6 @@ for spec in "eslint eslint_check" "stylelint stylelint_check" "prettier prettier bats_test_function --description "blocks npm exec -c running ${bin} after cd" \ -- js_admin_hook_blocks "npm exec --no -c \"cd ../.. && ${bin} src\"" "$tool" done -bats_test_function --description "blocks the npm exec -c form the MCP tools run for the components tree" \ - -- js_admin_hook_blocks 'npm exec --no -c "cd ../.. && eslint views/components"' "eslint_check" bats_test_function --description "blocks npx -c with a single-quoted string running stylelint" \ -- js_admin_hook_blocks "npx -c 'stylelint src'" "stylelint_check" bats_test_function --description "blocks npm exec --call running jest" \ diff --git a/plugin-tests/dev-tooling/js_storefront_tools.bats b/plugin-tests/dev-tooling/js_storefront_tools.bats index f07e96f..6973958 100644 --- a/plugin-tests/dev-tooling/js_storefront_tools.bats +++ b/plugin-tests/dev-tooling/js_storefront_tools.bats @@ -61,14 +61,12 @@ bats_test_function --description "blocks a bare npm run lint:js:fix → suggests -- js_storefront_hook_blocks "npm run lint:js:fix" "eslint_fix" bats_test_function --description "blocks a bare npm run lint:js → suggests eslint_check" \ -- js_storefront_hook_blocks "npm run lint:js" "eslint_check" -bats_test_function --description "blocks npm run lint:js:app → suggests eslint_check" \ - -- js_storefront_hook_blocks "npm run lint:js:app" "eslint_check" -# lint:js and lint:js:fix chain the per-tree scripts, but both components -# scripts are reachable on their own; the :fix suffix must route to the fixer. -bats_test_function --description "blocks npm run lint:js:components → suggests eslint_check" \ - -- js_storefront_hook_blocks "npm run lint:js:components" "eslint_check" -bats_test_function --description "blocks npm run lint:js:components:fix → suggests eslint_fix" \ - -- js_storefront_hook_blocks "npm run lint:js:components:fix" "eslint_fix" +# The Storefront `lint` script runs ESLint and then Stylelint; `lint-fix` runs +# both fixers. +bats_test_function --description "the npm run lint block names Stylelint beside ESLint" \ + -- js_storefront_hook_blocks "cd src/Storefront && npm run lint" "stylelint_check" +bats_test_function --description "blocks npm run lint-fix in Storefront context → suggests eslint_fix and stylelint_fix" \ + -- js_storefront_hook_blocks "cd src/Storefront && npm run lint-fix" "\`eslint_fix\` or \`stylelint_fix\`" bats_test_function --description "blocks npm run development in Storefront context → suggests webpack_build" \ -- js_storefront_hook_blocks "cd /app/storefront && npm run development" "webpack_build" bats_test_function --description "blocks npx jest in Storefront context → suggests jest_run" \ @@ -79,10 +77,14 @@ bats_test_function --description "blocks npm run unit:components:coverage → su -- js_storefront_hook_blocks "npm run unit:components:coverage" "vitest_run" bats_test_function --description "blocks npx vitest in Storefront context → suggests vitest_run" \ -- js_storefront_hook_blocks "cd src/Storefront && npx vitest" "vitest_run" +bats_test_function --description "blocks the npm exec -c form the MCP tools run for the components tree → suggests eslint_check" \ + -- js_storefront_hook_blocks 'npm exec --no -c "cd ../.. && eslint views/components"' "eslint_check" bats_test_function --description "blocks composer storefront:components:unit → suggests vitest_run" \ -- js_storefront_hook_blocks "composer storefront:components:unit" "vitest_run" bats_test_function --description "blocks composer ludtwig:storefront → suggests ludtwig_check" \ -- js_storefront_hook_blocks "composer ludtwig:storefront" "ludtwig_check" +bats_test_function --description "blocks composer -d ludtwig:storefront → suggests ludtwig_check" \ + -- js_storefront_hook_blocks "composer -d /app ludtwig:storefront" "ludtwig_check" bats_test_function --description "blocks composer ludtwig:storefront:fix → suggests ludtwig_fix" \ -- js_storefront_hook_blocks "composer ludtwig:storefront:fix" "ludtwig_fix" bats_test_function --description "blocks a bare ludtwig invocation → suggests ludtwig_check" \ @@ -105,20 +107,57 @@ bats_test_function --description "blocks a runner passing a flag and -- before l bats_test_function --description "blocks npm x running ludtwig" \ -- js_storefront_hook_blocks "npm x ludtwig src/Storefront/Resources/views" "ludtwig_check" -# The target-less base scripts. Shopware's Storefront package.json defines none -# of them and no MCP tool calls them; the redirects stay for a project that -# defines them. eslint:app, eslint:components and -# stylelint:app are Storefront names, so they are also Storefront context -# signals in their own right. -# bats test_tags=blocking,base-scripts -bats_test_function --description "blocks npm run eslint:app → suggests eslint_check" \ - -- js_storefront_hook_blocks "npm run eslint:app" "eslint_check" -bats_test_function --description "blocks npm run eslint:components → suggests eslint_check" \ - -- js_storefront_hook_blocks "npm run eslint:components" "eslint_check" -bats_test_function --description "blocks npm run stylelint:app → suggests stylelint_check" \ - -- js_storefront_hook_blocks "npm run stylelint:app" "stylelint_check" -bats_test_function --description "blocks npm run jest:base in Storefront context → suggests jest_run" \ - -- js_storefront_hook_blocks "cd src/Storefront && npm run jest:base" "jest_run" +# An env assignment ahead of npm, npm flags around `run`, and a `;` or `&&` +# glued to the script name still run it. The bare forms carry no +# tree marker, so the context detector has to recognize them as well. +# bats test_tags=blocking +bats_test_function --description "blocks npm run lint:js behind an env assignment in the Storefront tree → suggests eslint_check" \ + -- js_storefront_hook_blocks "cd storefront && CI=true npm run lint:js" "eslint_check" +bats_test_function --description "blocks npm run with a flag before unit in the Storefront tree → suggests jest_run" \ + -- js_storefront_hook_blocks "cd src/Storefront && npm run --silent unit" "jest_run" +bats_test_function --description "blocks a bare npm run lint:js glued to ; → suggests eslint_check" \ + -- js_storefront_hook_blocks "npm run lint:js;echo done" "eslint_check" +bats_test_function --description "blocks a bare npm run unit:components:coverage glued to && → suggests vitest_run" \ + -- js_storefront_hook_blocks "npm run unit:components:coverage&&echo done" "vitest_run" +bats_test_function --description "blocks composer run storefront:components:unit → suggests vitest_run" \ + -- js_storefront_hook_blocks "composer run storefront:components:unit" "vitest_run" +bats_test_function --description "blocks composer run ludtwig:storefront → suggests ludtwig_check" \ + -- js_storefront_hook_blocks "composer run ludtwig:storefront" "ludtwig_check" +bats_test_function --description "blocks composer run-script ludtwig:storefront:fix → suggests ludtwig_fix" \ + -- js_storefront_hook_blocks "composer run-script ludtwig:storefront:fix" "ludtwig_fix" +bats_test_function --description "blocks ludtwig behind an env assignment in the Storefront tree → suggests ludtwig_check" \ + -- js_storefront_hook_blocks "cd src/Storefront && CI=1 ludtwig ." "ludtwig_check" + +# A composer script or ludtwig glued to `;` still runs it. +# bats test_tags=blocking +bats_test_function --description "blocks composer run storefront:components:unit glued to ; → suggests vitest_run" \ + -- js_storefront_hook_blocks "composer run storefront:components:unit;echo done" "vitest_run" +bats_test_function --description "blocks a bare ludtwig glued to ; → suggests ludtwig_check" \ + -- js_storefront_hook_blocks "ludtwig;" "ludtwig_check" + +# bats test_tags=blocking +@test "composer ludtwig:storefront:fix glued to && is routed to ludtwig_fix, not ludtwig_check" { + run_hook "check-js-storefront-tools.sh" "composer ludtwig:storefront:fix&&true" + assert_failure 2 + assert_output --partial "ludtwig_fix" + refute_output --partial "ludtwig_check" +} + +# bats test_tags=blocking +@test "a bare npm run lint:js:fix glued to ; is routed to eslint_fix, not eslint_check" { + run_hook "check-js-storefront-tools.sh" "npm run lint:js:fix;echo done" + assert_failure 2 + assert_output --partial "eslint_fix" + refute_output --partial "eslint_check" +} + +# bats test_tags=allow +bats_test_function --description "allows npm run lint:js--x, a longer script name, in the Storefront tree" \ + -- storefront_hook_allows "cd src/Storefront && npm run lint:js--x" +bats_test_function --description "allows npm run unit:components:watchx, a longer script name, in the Storefront tree" \ + -- storefront_hook_allows "cd src/Storefront && npm run unit:components:watchx" +bats_test_function --description "allows composer run ludtwig:storefront-x, a longer script name" \ + -- storefront_hook_allows "composer run ludtwig:storefront-x" # A linter binary run through `npx`, `npm exec` or its node_modules/.bin path. # The MCP servers run the binaries themselves through `npm exec --no --`, so an @@ -151,6 +190,10 @@ bats_test_function --description "allows npm exec tsc in the Storefront tree, wh -- storefront_hook_allows "cd src/Storefront/Resources/app/storefront && npm exec --no -- tsc src" bats_test_function --description "allows npm exec vitest in the Administration tree" \ -- storefront_hook_allows "cd src/Administration/Resources/app/administration && npm exec --no -- vitest src" +# A Shopware plugin brings its own Vitest suite, which vitest_run refuses under +# a plugin scope, so a run naming no Storefront marker must stay unclaimed. +bats_test_function --description "allows npx vitest without a Storefront marker, which a plugin's own suite runs" \ + -- storefront_hook_allows "npx vitest run" bats_test_function --description "allows npm exec prettier in the Storefront tree, which has no prettier tool" \ -- storefront_hook_allows "cd src/Storefront/Resources/app/storefront && npm exec --no -- prettier --check src" bats_test_function --description "allows npm exec running another tool in the Storefront tree" \ @@ -162,15 +205,6 @@ bats_test_function --description "allows a linter name after another tool in an bats_test_function --description "allows a linter name inside quoted text after a semicolon in the Storefront tree" \ -- storefront_hook_allows 'cd src/Storefront && git commit -m "tidy; eslint config"' -# jest:base carries no side-specific marker. The Admin hook's unknown-context -# fallback owns the bare form, so this hook must decline it or the command -# would be blocked twice with two different tool names. -# bats test_tags=context,allow -@test "allows a bare npm run jest:base, which the Admin hook owns" { - run_hook "check-js-storefront-tools.sh" "npm run jest:base" - assert_success -} - # bats test_tags=blocking @test "npm run lint:js:fix is routed to eslint_fix, not eslint_check" { run_hook "check-js-storefront-tools.sh" "npm run lint:js:fix" @@ -178,13 +212,6 @@ bats_test_function --description "allows a linter name inside quoted text after refute_output --partial "eslint_check" } -# bats test_tags=blocking -@test "npm run lint:js:app:fix is routed to eslint_fix, not eslint_check" { - run_hook "check-js-storefront-tools.sh" "npm run lint:js:app:fix" - assert_failure 2 - refute_output --partial "eslint_check" -} - # bats test_tags=blocking @test "npm run unit:components is routed to vitest_run, not jest_run" { run_hook "check-js-storefront-tools.sh" "npm run unit:components" diff --git a/plugin-tests/dev-tooling/php_tools.bats b/plugin-tests/dev-tooling/php_tools.bats index 60c8dcc..7213263 100644 --- a/plugin-tests/dev-tooling/php_tools.bats +++ b/plugin-tests/dev-tooling/php_tools.bats @@ -26,20 +26,79 @@ bats_test_function --description "blocks && compound command → suggests phpsta -- php_hook_blocks "git pull && vendor/bin/phpstan analyze" "phpstan_analyze" bats_test_function --description "blocks composer phpstan → suggests phpstan_analyze" \ -- php_hook_blocks "composer phpstan -- src/" "phpstan_analyze" -bats_test_function --description "blocks composer ecs → suggests ecs_check/ecs_fix" \ - -- php_hook_blocks "composer ecs -- src/" "\`ecs_check\` or \`ecs_fix\`" +bats_test_function --description "blocks composer ecs → suggests ecs_check only" \ + -- php_hook_routes "composer ecs -- src/" "ecs_check" "ecs_fix" bats_test_function --description "blocks vendor/bin/rector → suggests rector_fix/rector_check" \ -- php_hook_blocks "vendor/bin/rector process src/" "\`rector_fix\` or \`rector_check\`" bats_test_function --description "blocks composer rector → suggests rector_fix/rector_check" \ -- php_hook_blocks "composer rector -- --dry-run" "\`rector_fix\` or \`rector_check\`" -bats_test_function --description "blocks composer rector-fix → suggests rector_fix/rector_check" \ - -- php_hook_blocks "composer rector-fix -- src/" "\`rector_fix\` or \`rector_check\`" -@test "blocks composer rector-fix without arguments" { - php_hook_blocks "composer rector-fix" "\`rector_fix\` or \`rector_check\`" -} +bats_test_function --description "blocks composer rector-fix → suggests rector_fix only" \ + -- php_hook_routes "composer rector-fix -- src/" "rector_fix" "rector_check" bats_test_function --description "blocks php bin/console → suggests console_run/console_list" \ -- php_hook_blocks "php bin/console cache:clear" "\`console_run\` or \`console_list\`" +# An environment assignment ahead of the command, and Composer's `run` / +# `run-script` subcommand ahead of a script name, still run the tool. +# bats test_tags=blocking +bats_test_function --description "blocks vendor/bin/phpunit behind an env assignment → suggests phpunit_run" \ + -- php_hook_blocks "APP_ENV=test vendor/bin/phpunit tests/" "phpunit_run" +bats_test_function --description "blocks bin/console behind an env assignment → suggests console_run/console_list" \ + -- php_hook_blocks "APP_ENV=test bin/console cache:clear" "\`console_run\` or \`console_list\`" +bats_test_function --description "blocks composer run phpstan → suggests phpstan_analyze" \ + -- php_hook_blocks "composer run phpstan" "phpstan_analyze" +bats_test_function --description "blocks composer run-script ecs-fix → suggests ecs_fix only" \ + -- php_hook_routes "composer run-script ecs-fix" "ecs_fix" "ecs_check" +bats_test_function --description "blocks composer run rector → suggests rector_fix/rector_check" \ + -- php_hook_blocks "composer run rector" "\`rector_fix\` or \`rector_check\`" +bats_test_function --description "blocks composer -d phpstan → suggests phpstan_analyze" \ + -- php_hook_blocks "composer -d src/Core phpstan" "phpstan_analyze" +bats_test_function --description "blocks composer --working-dir= run phpunit → suggests phpunit_run" \ + -- php_hook_blocks "composer --working-dir=src/Core run phpunit" "phpunit_run" + +# A `;` or `|` glued to the tool or script name still runs it. +# bats test_tags=blocking +bats_test_function --description "blocks vendor/bin/phpunit glued to ; → suggests phpunit_run" \ + -- php_hook_blocks "vendor/bin/phpunit;echo" "phpunit_run" +bats_test_function --description "blocks composer run rector glued to | → suggests rector_fix/rector_check" \ + -- php_hook_blocks "composer run rector|tee out.txt" "\`rector_fix\` or \`rector_check\`" + +# Shopware core's current Composer scripts: `cs` / `cs-fix` replace the +# deprecated `ecs` alias, and `rector-fix` applies Rector. Each routes to the one +# tool that does the same thing. +# bats test_tags=blocking +php_hook_routes() { + php_hook_blocks "$1" "\`$2\` on the" + refute_output --partial "\`$3\`" +} +bats_test_function --description "blocks composer cs → suggests ecs_check only" \ + -- php_hook_routes "composer cs" "ecs_check" "ecs_fix" +bats_test_function --description "blocks composer run cs-fix glued to ; → suggests ecs_fix only" \ + -- php_hook_routes "composer run cs-fix;true" "ecs_fix" "ecs_check" +bats_test_function --description "blocks composer rector-fix behind an env assignment → suggests rector_fix only" \ + -- php_hook_routes "XDEBUG_MODE=off composer rector-fix" "rector_fix" "rector_check" + +# A longer script name and text inside a quoted commit message are not a run. +# bats test_tags=allow +php_hook_allows() { + run_hook "check-php-tools.sh" "$1" + assert_success + assert_output "" +} +bats_test_function --description "allows composer phpstan-baseline, a longer script name" \ + -- php_hook_allows "composer phpstan-baseline" +bats_test_function --description "allows composer run phpstan-baseline, a longer script name" \ + -- php_hook_allows "composer run phpstan-baseline" +bats_test_function --description "allows composer csv-import, a longer script name" \ + -- php_hook_allows "composer csv-import" +bats_test_function --description "allows composer cs-fixer-something, a longer script name" \ + -- php_hook_allows "composer cs-fixer-something" +bats_test_function --description "allows composer rector-fixture, a longer script name" \ + -- php_hook_allows "composer rector-fixture" +bats_test_function --description "allows composer run phpstan named inside a quoted commit message" \ + -- php_hook_allows 'git commit -m "document composer run phpstan"' +bats_test_function --description "allows an env-prefixed phpunit run named inside a quoted commit message" \ + -- php_hook_allows 'git commit -m "use APP_ENV=test vendor/bin/phpunit"' + # bats test_tags=cwd @test "reads the config from the hook input cwd when CLAUDE_PROJECT_DIR is unset" { unset CLAUDE_PROJECT_DIR @@ -82,3 +141,44 @@ bats_test_function --description "blocks php bin/console → suggests console_ru run_hook "check-php-tools.sh" "vendor/bin/phpstan analyze" assert_success } + +# The servers merge every config file they find, later locations winning, so +# a `.claude/` file without the key leaves the root file's opt-out in force. +# bats test_tags=config +@test "allows all when the root config opts out and a .claude config omits the key" { + setup_config "php-tooling" '{"environment": "native", "enforce_mcp_tools": false}' + mkdir -p "${BATS_TEST_TMPDIR}/.claude" + echo '{"environment": "native"}' > "${BATS_TEST_TMPDIR}/.claude/.mcp-php-tooling.json" + run_hook "check-php-tools.sh" "vendor/bin/phpstan analyze" + assert_success +} + +# bats test_tags=config +@test "allows all when the MCP_PHP_TOOLING_CONFIG file opts out over an enforcing project config" { + local override="${BATS_TEST_TMPDIR}/override.json" + echo '{"environment": "native", "enforce_mcp_tools": false}' > "$override" + MCP_PHP_TOOLING_CONFIG="$override" run_hook "check-php-tools.sh" "vendor/bin/phpstan analyze" + assert_success +} + +# An empty file holds no config, and the merge alone would drop it and let the +# root file's opt-out decide. An unreadable config reads as no config, so the +# defaults apply and enforcement stays on. +# bats test_tags=config +@test "blocks when a .claude config is empty and the root config opts out" { + setup_config "php-tooling" '{"environment": "native", "enforce_mcp_tools": false}' + mkdir -p "${BATS_TEST_TMPDIR}/.claude" + : > "${BATS_TEST_TMPDIR}/.claude/.mcp-php-tooling.json" + run_hook "check-php-tools.sh" "vendor/bin/phpstan analyze" + assert_failure 2 + assert_output --partial "phpstan_analyze" +} + +# Each line is matched on its own, so a blocked command on a later line is +# blocked too. +# bats test_tags=blocking +@test "blocks phpunit run after a heredoc's terminator line" { + run_hook "check-php-tools.sh" $'cat > notes.txt < php scopes still present" { + cat > "${BATS_TEST_TMPDIR}/.mcp-php-tooling.json" <<'JSON' +{"enforce_mcp_tools":false,"scopes":{"plugin-x":{"cwd":"custom/plugins/X"}}} +JSON + cat > "${BATS_TEST_TMPDIR}/.mcp-js-tooling.json" <<'JSON' +{"scopes":{"plugin-j":{"cwd":"custom/plugins/J"}}} +JSON + run bash -c "echo '{}' | ${SCRIPT}" + assert_success + assert_output --partial "## php-tooling scopes" + assert_output --partial "plugin-x" + refute_output --partial "phpstan_analyze" + assert_output --partial "## js-tooling scopes" + assert_output --partial "plugin-j" +} + @test "session-start: no default_scope -> implicit shopware shown" { cat > "${BATS_TEST_TMPDIR}/.mcp-php-tooling.json" <<'JSON' {"environment":"native","scopes":{"plugin-x":{"cwd":"custom/plugins/X"}}} @@ -62,3 +81,14 @@ JSON assert_success assert_output --partial "Default scope: shopware" } + +# The servers merge every config file they find, so scopes declared in the root +# file stay in force when a `.claude/` file sets other keys. +@test "session-start: root scopes plus a .claude config without scopes -> section rendered" { + _write_two_scopes_config + mkdir -p "${BATS_TEST_TMPDIR}/.claude" + echo '{"environment":"native"}' > "${BATS_TEST_TMPDIR}/.claude/.mcp-php-tooling.json" + run bash -c "echo '{}' | ${SCRIPT}" + assert_success + assert_output --partial "Declared scopes: shopware (implicit), plugin-x, plugin-y" +} diff --git a/plugin-tests/dev-tooling/session_start.bats b/plugin-tests/dev-tooling/session_start.bats index 5de8a67..d0a4aa6 100644 --- a/plugin-tests/dev-tooling/session_start.bats +++ b/plugin-tests/dev-tooling/session_start.bats @@ -55,6 +55,132 @@ run_session_start() { echo "$output" | jq -e '.hookSpecificOutput.additionalContext | length > 0' } +# The servers merge every config file they find, later locations winning, so +# a `.claude/` file without the key leaves the root file's opt-out in force. +# bats test_tags=config +@test "silent when the root configs opt out and .claude configs omit the key" { + setup_config "php-tooling" '{"enforce_mcp_tools": false}' + setup_config "js-tooling" '{"enforce_mcp_tools": false}' + mkdir -p "${BATS_TEST_TMPDIR}/.claude" + echo '{"environment": "native"}' > "${BATS_TEST_TMPDIR}/.claude/.mcp-php-tooling.json" + echo '{"environment": "native"}' > "${BATS_TEST_TMPDIR}/.claude/.mcp-js-tooling.json" + run_session_start + assert_success + assert_output "" +} + +# bats test_tags=config +@test "silent when the MCP_*_CONFIG files opt out over enforcing project configs" { + echo '{"enforce_mcp_tools": false}' > "${BATS_TEST_TMPDIR}/php-override.json" + echo '{"enforce_mcp_tools": false}' > "${BATS_TEST_TMPDIR}/js-override.json" + MCP_PHP_TOOLING_CONFIG="${BATS_TEST_TMPDIR}/php-override.json" \ + MCP_JS_TOOLING_CONFIG="${BATS_TEST_TMPDIR}/js-override.json" \ + run_session_start + assert_success + assert_output "" +} + +# Each server's directives follow its own enforce_mcp_tools: the opted-out side +# loses its language in the lead line and its tool lines, the other keeps both. +# bats test_tags=config +@test "omits the PHP directives when only php enforcement is disabled" { + setup_config "php-tooling" '{"environment": "native", "enforce_mcp_tools": false}' + setup_config "js-tooling" '{}' + run_session_start + assert_success + run jq -r '.hookSpecificOutput.additionalContext' <<< "$output" + assert_success + assert_line --index 0 "ALWAYS use MCP dev tools for JavaScript operations — NEVER run these via Bash." + refute_output --partial "phpstan_analyze" + assert_output --partial "On the \`js-admin-tooling\` MCP server: \`eslint_check\`" + assert_output --partial "On the \`js-storefront-tooling\` MCP server: \`eslint_check\`" +} + +# bats test_tags=config +@test "omits the JavaScript directives when only js enforcement is disabled" { + setup_config "php-tooling" '{}' + setup_config "js-tooling" '{"environment": "native", "enforce_mcp_tools": false}' + run_session_start + assert_success + run jq -r '.hookSpecificOutput.additionalContext' <<< "$output" + assert_success + assert_line --index 0 "ALWAYS use MCP dev tools for PHP operations — NEVER run these via Bash." + assert_output --partial "On the \`php-tooling\` MCP server: \`phpstan_analyze\`" + refute_output --partial "On the \`js-admin-tooling\` MCP server:" + refute_output --partial "On the \`js-storefront-tooling\` MCP server:" +} + +# Beyond the tool lines, no paragraph may steer to the opted-out language's +# tools: the worktree ludtwig note naming `php-tooling`, the cross-server +# parallel example, the runner's per-language check lists, the Storefront test +# split. The language-neutral guidance stays. The Codex bind hint is cut off +# first: it names every server by design and is not a directive. +directives_for_opt_out() { + local opted_out="$1" host="$2" + local project="${BATS_TEST_TMPDIR}/opt-out-project" + mkdir -p "$project" + echo '{"enforce_mcp_tools": false}' > "${project}/.mcp-${opted_out}.json" + if [[ "$host" == "codex" ]]; then + unset CLAUDE_PROJECT_DIR + else + export CLAUDE_PROJECT_DIR="$project" + fi + run_session_start_with_input "$(jq -cn --arg cwd "$project" '{cwd: $cwd}')" + assert_success + run jq -r '.hookSpecificOutput.additionalContext | split("\n\n## Project root for the dev-tooling MCP servers")[0]' <<< "$output" + assert_success + refute_output --partial $'\n\n\n' +} + +# bats test_tags=config +@test "a php opt-out leaves no PHP-steering text in the Claude directives" { + directives_for_opt_out "php-tooling" "claude" + refute_output --partial "\`php-tooling\`" + refute_output --partial "phpunit_run" + refute_output --partial "PHPStan" + refute_output --partial "ecs_fix" + assert_output --partial "delegate it to the \`dev-tooling-runner\` subagent" + assert_output --partial "NEVER symlink vendor/ from the launch tree" + assert_output --partial "Call tools on the SAME server sequentially" + assert_output --partial "\`vitest_run\` on the \`js-storefront-tooling\` MCP server covers the component suite" +} + +# bats test_tags=config +@test "a js opt-out leaves no JavaScript-steering text in the Claude directives" { + directives_for_opt_out "js-tooling" "claude" + refute_output --partial "\`js-admin-tooling\`" + refute_output --partial "\`js-storefront-tooling\`" + refute_output --partial "jest_run" + refute_output --partial "ESLint" + refute_output --partial "ludtwig" + assert_output --partial "delegate it to the \`dev-tooling-runner\` subagent" + assert_output --partial "NEVER symlink vendor/ from the launch tree" + assert_output --partial "Call tools on the SAME server sequentially" + assert_output --partial "On the \`php-tooling\` MCP server: \`phpstan_analyze\`" +} + +# bats test_tags=config,cwd +@test "a php opt-out leaves no PHP-steering text in the Codex directives" { + directives_for_opt_out "php-tooling" "codex" + refute_output --partial "\`php-tooling\`" + refute_output --partial "phpunit_run" + assert_output --partial "do not symlink vendor/ from the launch tree" + assert_output --partial "Call tools on the same server one at a time." + assert_output --partial "\`vitest_run\` on the \`js-storefront-tooling\` MCP server covers the component suite" +} + +# bats test_tags=config,cwd +@test "a js opt-out leaves no JavaScript-steering text in the Codex directives" { + directives_for_opt_out "js-tooling" "codex" + refute_output --partial "\`js-admin-tooling\`" + refute_output --partial "\`js-storefront-tooling\`" + refute_output --partial "jest_run" + refute_output --partial "ludtwig" + assert_output --partial "do not symlink vendor/ from the launch tree" + assert_output --partial "Call tools on the same server one at a time." + assert_output --partial "On the \`php-tooling\` MCP server: \`phpstan_analyze\`" +} + @test "outputs when no config files exist" { export CLAUDE_PROJECT_DIR="${BATS_TEST_TMPDIR}/empty" mkdir -p "$CLAUDE_PROJECT_DIR" diff --git a/plugin-tests/dev-tooling/test_helper/common_setup.bash b/plugin-tests/dev-tooling/test_helper/common_setup.bash index 6b02cc3..b3f0d2a 100644 --- a/plugin-tests/dev-tooling/test_helper/common_setup.bash +++ b/plugin-tests/dev-tooling/test_helper/common_setup.bash @@ -16,7 +16,10 @@ setup_config() { } # Default setup - override CONFIG_PREFIX in test file +# The override variables point every server at one file, so a value exported in +# the developer's shell would decide the config these tests read. setup() { + unset MCP_PHP_TOOLING_CONFIG MCP_JS_TOOLING_CONFIG setup_config "${CONFIG_PREFIX:-php-tooling}" '{"environment": "native"}' } diff --git a/plugin-tests/shopware-env/test_helper/common_setup.bash b/plugin-tests/shopware-env/test_helper/common_setup.bash index a88d90f..6ab34ed 100644 --- a/plugin-tests/shopware-env/test_helper/common_setup.bash +++ b/plugin-tests/shopware-env/test_helper/common_setup.bash @@ -54,6 +54,7 @@ setup_lifecycle_mcp_env() { } setup() { + unset MCP_PHP_TOOLING_CONFIG MCP_JS_TOOLING_CONFIG setup_config "php-tooling" '{"environment": "native"}' } diff --git a/plugins/dev-tooling/docs/mcp-enforcement.md b/plugins/dev-tooling/docs/mcp-enforcement.md index 53df01f..e8617ad 100644 --- a/plugins/dev-tooling/docs/mcp-enforcement.md +++ b/plugins/dev-tooling/docs/mcp-enforcement.md @@ -19,7 +19,7 @@ Three hook events carry scripts between them: SessionStart, PreToolUse, and Post **PostToolUse** runs two. One watches `phpstan_analyze`: when it runs against specific files, it cross-references `phpstan-baseline.neon` (or `.php`) and surfaces a warning if any of the analyzed paths appear in the baseline, which usually means a baseline entry has gone stale. Full-project PHPStan runs skip the check because PHPStan validates the baseline natively there. Its matcher carries both host spellings of the tool name, so it fires for a Claude Code plugin install and for a Codex install alike. The other watches `EnterWorktree` and `ExitWorktree` — both Claude Code tools — and reminds the session to call `set_project_root` on all three servers, since each holds its own sticky root. -The SessionStart script, `session-start.sh`, honors `enforce_mcp_tools` and turns off when it's `false`; the project-root hint above is not gated on the flag, because a server started without a project root refuses every tool whatever the flag says. Every PreToolUse script honors the flag. Both PostToolUse scripts, the baseline check and the worktree-directives reminder, ignore the flag and always run. +The SessionStart script, `session-start.sh`, honors `enforce_mcp_tools`: `false` drops the directive lines for that language. The scopes section it renders from the same config stays, because scopes describe what the tools accept rather than steering to them. The project-root hint is not gated on the flag either, because a server started without a project root refuses every tool whatever the flag says. Every PreToolUse script honors the flag. Both PostToolUse scripts, the baseline check and the worktree-directives reminder, ignore the flag and always run. On Codex, hooks run only once you trust them: its startup review and the `/hooks` command each record trust in your Codex user config, an untrusted hook is skipped, and a non-interactive `codex exec` runs the trusted hooks without asking or all of them with `--dangerously-bypass-hook-trust`. All of them are plugin hooks, so none runs there until you trust it. @@ -31,66 +31,74 @@ Flip the switch per config file (`.mcp-php-tooling.json` or `.mcp-js-tooling.jso { "environment": "native", "enforce_mcp_tools": false } ``` +The hooks find the configuration the way the servers do: the file named by `MCP_PHP_TOOLING_CONFIG` or `MCP_JS_TOOLING_CONFIG` when that variable is set, otherwise every `.mcp-.json` at the discovery locations, deep-merged in the servers' order. A missing override file, an empty file, and a file that does not parse as JSON or whose value is not an object all make the hook read no configuration at all, which leaves enforcement on. + ### Blocked PHP Commands -| Bash Command | MCP Tool on `php-tooling` | -|--------------------------------------------------------------------|--------------------------------| -| `vendor/bin/phpstan`, `composer phpstan` | `phpstan_analyze` | -| `vendor/bin/ecs`, `vendor/bin/php-cs-fixer`, `composer ecs` | `ecs_check` / `ecs_fix` | -| `vendor/bin/phpunit`, `composer phpunit` | `phpunit_run` | -| `bin/console`, `php bin/console` | `console_run` / `console_list` | -| `vendor/bin/rector`, `composer rector`, `composer rector-fix` | `rector_fix` / `rector_check` | +| Bash Command | MCP Tool on `php-tooling` | +|---------------------------------------------------------------------------------|--------------------------------| +| `vendor/bin/phpstan`, `composer phpstan` | `phpstan_analyze` | +| `vendor/bin/ecs`, `vendor/bin/php-cs-fixer` | `ecs_check` / `ecs_fix` | +| `composer ecs` | `ecs_check` | +| `composer ecs-fix` | `ecs_fix` | +| `composer cs` | `ecs_check` | +| `composer cs-fix` | `ecs_fix` | +| `vendor/bin/phpunit`, `composer phpunit` | `phpunit_run` | +| `bin/console`, `php bin/console` | `console_run` / `console_list` | +| `vendor/bin/rector`, `composer rector` | `rector_fix` / `rector_check` | +| `composer rector-fix` | `rector_fix` | + +Composer scripts match with or without `run` or `run-script`, behind `NAME=value` assignments, and after global options such as `-d ` or `--working-dir=`, so `FOO=1 composer -d /tmp run cs` is blocked. ### Blocked JavaScript Commands -The JS hook picks Admin vs Storefront from the command itself and redirects to the matching server. Detection is by path pattern first, then by script names that belong to one side — `lint:js`, `production`, `development`, `unit:components`, `eslint:app`, `eslint:components` and `stylelint:app` for Storefront, plus a bare `ludtwig`, which lints Storefront Twig templates and nothing else. `jest:base` carries no side-specific marker, so it cannot be classified this way: a command that also names the Storefront tree (path pattern or another Storefront-only script) is claimed by the Storefront hook, and every other `jest:base` invocation — including a bare one — falls to the Admin hook's unknown-context default. +The JS hook picks Administration vs Storefront from the command itself and redirects to the matching server. Each of the two hooks decides on its own. The Administration hook claims a command that names `Administration` or `/app/administration`, or one of its scripts `lint:types`, `format`, `lint:twig` and `lint:all`; it declines a command that names `Storefront`, `/app/storefront` or `views/components`, or one of the Storefront scripts `lint:js`, `production` or `development`, and claims everything else as the unknown-context default. The Storefront hook claims a command that names `Storefront`, `/app/storefront` or `views/components`, one of its scripts `lint:js` (with any letters-only `:` segment, such as `lint:js:fix`), `production`, `development` or `unit:components` (with `:watch` or `:coverage`), or a `ludtwig` token, which lints Storefront Twig templates and nothing else. Its Composer scripts match the way the PHP hook's do: with or without `run`, behind `NAME=value` assignments, and after global options such as `-d `. A Vitest run that names no Storefront marker is blocked by neither hook. A plugin's Vitest run that names its `app/storefront` path is blocked and pointed at `vitest_run`, which runs only the core Storefront component suite and refuses every plugin scope. > [!NOTE] -> Detection reads the whole command string, so a compound command that names both trees — `git diff -- src/Administration src/Storefront && npm run jest:base` — satisfies both detectors and is blocked twice, once per server. This applies to any command mentioning both trees, not only the base scripts. +> Detection reads the whole command string, so a compound command that names both trees — `ls src/Administration src/Storefront && npm run lint` — satisfies both detectors and is blocked twice, once per server. This applies to any command that names both trees and runs a command both hooks block. Tool binaries are blocked when run directly at a command position, in three forms: - `npx `, `npm exec ` or its alias `npm x `, with an optional version suffix (`npx eslint@9`). -- `/node_modules/.bin/`. +- `/node_modules/.bin/`, directly or through `node` (`node node_modules/.bin/eslint`). - The command-string form, `npx -c ''`, `npm exec -c ""` or `--call`, when the string runs the binary at its start or after `;`, `&&` or `|` (`npm exec --no -c "cd ../.. && eslint views/components"`). The binaries are `eslint`, `stylelint` and `jest` on both servers, `prettier` and `tsc` on Administration only, and `vitest` on Storefront only. Between the runner and the binary the hooks look past `-y`, `--yes`, `--no`, `--no-install`, `-q`, `--quiet`, `--silent` and `--`. Before `-c` they look past the same flags except `--`, plus `--package`, `-p`, `--workspace` and `-w` with their value. +npm scripts match the whole name: `npm run lint` matches `lint` and not `lint:fix` or `lint:js--x`. The hooks look past `NAME=value` assignments, npm flags that take no value before and after `run`, and `--prefix ` before `run`, so `FOO=1 npm run --silent lint` and `npm --prefix . run lint` are blocked. Any other flag with a value, such as `npm --workspace foo run lint`, is not looked past. + Not blocked: - A flag that takes a value before the binary (`npx --package=eslint eslint`, `npm exec --workspace foo -- eslint`). Reading its value as the binary would also block `npm exec --package eslint some-other-tool`. - A bare binary name (`eslint`, `jest`). A command-position match would block a commit message containing `; eslint config`. - A binary name in an argument. -[reference.md](./reference.md) describes how each MCP tool routes its own path-scoped runs. The table still lists `stylelint:base`, `prettier:base`, `jest:base`, `eslint:app`, `eslint:components` and `stylelint:app`, which Shopware's `package.json` files do not define, because a project can define them. - -| Bash Command | Admin MCP Tool | Storefront MCP Tool | -|------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------|---------------------| -| `npm run lint`, `npx eslint`, `npm exec eslint`, `node_modules/.bin/eslint` | `eslint_check` | `eslint_check` | -| `npm run lint:js` | N/A | `eslint_check` | -| `npm run lint:debugging` | `eslint_check` | N/A | -| `npm run eslint:app`, `npm run eslint:components` | N/A | `eslint_check` | -| `npm run lint:fix` | `eslint_fix` | `eslint_fix` | -| `npm run lint:js:fix` | N/A | `eslint_fix` | -| `npm run lint:scss`, `npx stylelint`, `npm exec stylelint`, `node_modules/.bin/stylelint` | `stylelint_check` | `stylelint_check` | -| `npm run stylelint:base` | `stylelint_check` | N/A | -| `npm run stylelint:app` | N/A | `stylelint_check` | -| `npm run lint:scss-fix` | `stylelint_fix` | `stylelint_fix` | -| `npm run format`, `npx prettier`, `npm exec prettier`, `node_modules/.bin/prettier` | `prettier_check` | N/A (Admin only) | -| `npm run prettier:base` | `prettier_check` | N/A (Admin only) | -| `npm run format:fix` | `prettier_fix` | N/A (Admin only) | -| `npm run unit`, `npx jest`, `npm exec jest`, `node_modules/.bin/jest` | `jest_run` | `jest_run` | -| `npm run jest:base` | `jest_run` | `jest_run` | -| `npm run lint:types`, `npx tsc`, `npm exec tsc`, `node_modules/.bin/tsc` | `tsc_check` | N/A (Admin only) | -| `npm run lint:all` | `lint_all` | N/A (Admin only) | -| `npm run lint:twig` | `lint_twig` | N/A (Admin only) | -| `npm run build` | `vite_build` | N/A | -| `npm run unit:components` (and `:watch` / `:coverage`), `npx vitest`, `npm exec vitest`, `node_modules/.bin/vitest`, `composer storefront:components:unit` | N/A | `vitest_run` | -| `composer ludtwig:storefront`, bare `ludtwig` | N/A | `ludtwig_check` | -| `composer ludtwig:storefront:fix` | N/A | `ludtwig_fix` | -| `npm run production/development` | N/A | `webpack_build` | - -`npm run unit` and `npm run unit:components` are matched by separate patterns, so the component suite is redirected to `vitest_run` and never to `jest_run`. The `:fix` ludtwig pattern is tested before the plain one, so `composer ludtwig:storefront:fix` resolves to `ludtwig_fix` rather than being shadowed by `ludtwig_check`. +[reference.md](./reference.md) describes how each MCP tool routes its own path-scoped runs. A command in the table is blocked by a hook only when that hook claims it, as described above: a bare `npm run lint-fix` names no Storefront marker, so only the Administration hook blocks it. + +| Bash Command | Admin MCP Tool | Storefront MCP Tool | +|------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------|------------------------------------| +| `npm run lint` | `eslint_check` | `eslint_check` / `stylelint_check` | +| `npx eslint`, `npm exec eslint`, `node_modules/.bin/eslint` | `eslint_check` | `eslint_check` | +| `npm run lint:js` | N/A | `eslint_check` | +| `npm run lint:debugging` | `eslint_check` | N/A | +| `npm run lint:fix` | `eslint_fix` | N/A | +| `npm run lint-fix` | `eslint_fix` | `eslint_fix` / `stylelint_fix` | +| `npm run lint:js:fix` | N/A | `eslint_fix` | +| `npm run lint:scss`, `npx stylelint`, `npm exec stylelint`, `node_modules/.bin/stylelint` | `stylelint_check` | `stylelint_check` | +| `npm run lint:scss-fix` | `stylelint_fix` | `stylelint_fix` | +| `npm run format`, `npx prettier`, `npm exec prettier`, `node_modules/.bin/prettier` | `prettier_check` | N/A (Admin only) | +| `npm run format:fix` | `prettier_fix` | N/A (Admin only) | +| `npm run unit`, `npx jest`, `npm exec jest`, `node_modules/.bin/jest` | `jest_run` | `jest_run` | +| `npm run lint:types`, `npx tsc`, `npm exec tsc`, `node_modules/.bin/tsc` | `tsc_check` | N/A (Admin only) | +| `npm run lint:all` | `lint_all` | N/A (Admin only) | +| `npm run lint:twig` | `lint_twig` | N/A (Admin only) | +| `npm run build` | `vite_build` | N/A | +| `npm run unit:components` (and `:watch` / `:coverage`), `npx vitest`, `npm exec vitest`, `node_modules/.bin/vitest`, `composer storefront:components:unit` | N/A | `vitest_run` | +| `composer ludtwig:storefront`, bare `ludtwig` | N/A | `ludtwig_check` | +| `composer ludtwig:storefront:fix` | N/A | `ludtwig_fix` | +| `npm run production/development` | N/A | `webpack_build` | + +`npm run unit` and `npm run unit:components` are matched by separate patterns, so the component suite is redirected to `vitest_run` and never to `jest_run`. The Vitest row applies in the Storefront tree only: a Vitest run that names no Storefront marker is blocked by neither hook. The `:fix` ludtwig pattern is tested before the plain one, so `composer ludtwig:storefront:fix` resolves to `ludtwig_fix` rather than being shadowed by `ludtwig_check`. In the Storefront tree `lint` runs ESLint and then Stylelint, and `lint-fix` runs both fixers. The Storefront hook names both tools for those two scripts. Commands that aren't blocked: `npm install`, `composer install`, watch-mode scripts other than `npm run unit:components:watch`, any npm script that doesn't match one of the patterns above, the bare binary names described above, and `npx`, `npm exec` or `npm x` running a tool not named in the table. diff --git a/plugins/dev-tooling/hooks/prompts/mcp-tool-directives-codex.md b/plugins/dev-tooling/hooks/prompts/mcp-tool-directives-codex.md index 982fa9e..c9b40e1 100644 --- a/plugins/dev-tooling/hooks/prompts/mcp-tool-directives-codex.md +++ b/plugins/dev-tooling/hooks/prompts/mcp-tool-directives-codex.md @@ -6,8 +6,10 @@ On the `js-storefront-tooling` MCP server: `eslint_check`/`eslint_fix`, `styleli A server started without a project root refuses every tool until one is bound, except the two that manage it: `set_project_root` on each dev-tooling MCP server and `cwd` on each dev-tooling MCP server. The root a server is bound to, by that call or at startup, is its launch root. The final section of this context, when present, names the project directory and the call that binds a server; without it, bind to the session's working directory. -Every tool except `cwd` on each dev-tooling MCP server takes an optional project_root to target a linked git worktree of the launch root, in every environment; refusals name what to fix and how. To work in a worktree across calls, call `set_project_root` on each dev-tooling MCP server you use with the worktree path, or with none to clear; each server is a separate process with its own sticky value, and `cwd` on each dev-tooling MCP server reports that server's current state. A fresh worktree lacks vendor/ and node_modules: call `worktree_prepare` on the dev-tooling MCP server the refusal names (a ludtwig refusal on a missing vendor/ names `php-tooling`) instead of provisioning by hand, and do not symlink vendor/ from the launch tree (composer's autoloaders resolve through the symlink to the launch tree's classes). +Every tool except `cwd` on each dev-tooling MCP server takes an optional project_root to target a linked git worktree of the launch root, in every environment; refusals name what to fix and how. To work in a worktree across calls, call `set_project_root` on each dev-tooling MCP server you use with the worktree path, or with none to clear; each server is a separate process with its own sticky value, and `cwd` on each dev-tooling MCP server reports that server's current state. A fresh worktree lacks vendor/ and node_modules: call `worktree_prepare` on the dev-tooling MCP server the refusal names instead of provisioning by hand, and do not symlink vendor/ from the launch tree (composer's autoloaders resolve through the symlink to the launch tree's classes). +A ludtwig refusal on the `js-storefront-tooling` MCP server over a missing vendor/ names `php-tooling`. Storefront tests are split across two runners: `jest_run` on the `js-storefront-tooling` MCP server covers the app/storefront package suite, `vitest_run` on the `js-storefront-tooling` MCP server covers the component suite under src/Storefront/Resources/views/components/. `jest_run` on the `js-storefront-tooling` MCP server rejects views/components patterns. -Call tools on the same server one at a time. Tools on different servers can run in parallel (e.g. `phpunit_run` on the `php-tooling` MCP server + `jest_run` on the `js-storefront-tooling` MCP server). +Call tools on the same server one at a time. Tools on different servers can run in parallel. +Example: `phpunit_run` on the `php-tooling` MCP server + `jest_run` on the `js-storefront-tooling` MCP server. diff --git a/plugins/dev-tooling/hooks/prompts/mcp-tool-directives.md b/plugins/dev-tooling/hooks/prompts/mcp-tool-directives.md index b00d977..4c1e26e 100644 --- a/plugins/dev-tooling/hooks/prompts/mcp-tool-directives.md +++ b/plugins/dev-tooling/hooks/prompts/mcp-tool-directives.md @@ -6,10 +6,14 @@ On the `php-tooling` MCP server: `phpstan_analyze`, `ecs_check`, `ecs_fix`, `php On the `js-admin-tooling` MCP server: `eslint_check`/`eslint_fix`, `stylelint_check`/`stylelint_fix`, `prettier_check`/`prettier_fix`, `jest_run`, `tsc_check`, `lint_all`, `lint_twig`, `unit_setup`, `vite_build`, `worktree_prepare`, `set_project_root`, `cwd` On the `js-storefront-tooling` MCP server: `eslint_check`/`eslint_fix`, `stylelint_check`/`stylelint_fix`, `jest_run`, `vitest_run`, `ludtwig_check`/`ludtwig_fix`, `webpack_build`, `worktree_prepare`, `set_project_root`, `cwd` -Every tool except `cwd` on each dev-tooling MCP server takes an optional project_root to target a linked git worktree of the launch root, in every environment; refusals name what to fix and how. After EnterWorktree/ExitWorktree, call `set_project_root` on each dev-tooling MCP server (with the worktree path, or with none to clear) — each is a separate process with its own sticky value; `cwd` on each dev-tooling MCP server reports that server's current state. A fresh worktree lacks vendor/ and node_modules — call `worktree_prepare` on the dev-tooling MCP server the refusal names (a ludtwig refusal on a missing vendor/ names `php-tooling`) instead of provisioning by hand, and NEVER symlink vendor/ from the launch tree (composer's autoloaders resolve through the symlink to the launch tree's classes). +Every tool except `cwd` on each dev-tooling MCP server takes an optional project_root to target a linked git worktree of the launch root, in every environment; refusals name what to fix and how. After EnterWorktree/ExitWorktree, call `set_project_root` on each dev-tooling MCP server (with the worktree path, or with none to clear) — each is a separate process with its own sticky value; `cwd` on each dev-tooling MCP server reports that server's current state. A fresh worktree lacks vendor/ and node_modules — call `worktree_prepare` on the dev-tooling MCP server the refusal names instead of provisioning by hand, and NEVER symlink vendor/ from the launch tree (composer's autoloaders resolve through the symlink to the launch tree's classes). +A ludtwig refusal on the `js-storefront-tooling` MCP server over a missing vendor/ names `php-tooling`. Storefront tests are split across two runners: `jest_run` on the `js-storefront-tooling` MCP server covers the app/storefront package suite, `vitest_run` on the `js-storefront-tooling` MCP server covers the component suite under src/Storefront/Resources/views/components/. `jest_run` on the `js-storefront-tooling` MCP server rejects views/components patterns. -Call tools on the SAME server sequentially — never in parallel. Tools on DIFFERENT servers CAN run in parallel (e.g. `phpunit_run` on the `php-tooling` MCP server + `jest_run` on the `js-storefront-tooling` MCP server). +Call tools on the SAME server sequentially — never in parallel. Tools on DIFFERENT servers CAN run in parallel. +Example: `phpunit_run` on the `php-tooling` MCP server + `jest_run` on the `js-storefront-tooling` MCP server. -When a dev-tooling run (PHPStan, ECS, PHPUnit, Rector, ESLint, Stylelint, Prettier, TypeScript, Jest, Vitest, ludtwig, or a Vite/Webpack build) would produce more than a trivial single check — especially during a large implementation task — delegate it to the `dev-tooling-runner` subagent so the verbose output stays out of this conversation. You decide what to check: hand it the explicit paths and any affected tests, plus the kinds of checks to run and any mechanical fixes (e.g. `ecs_fix` on the `php-tooling` MCP server) to apply. It executes and returns a condensed pass/fail report. For a quick single-file check you may still call the MCP tool inline. +When a dev-tooling run would produce more than a trivial single check — especially during a large implementation task — delegate it to the `dev-tooling-runner` subagent so the verbose output stays out of this conversation. You decide what to check: hand it the explicit paths and any affected tests, plus the kinds of checks to run and any mechanical fixes to apply. It executes and returns a condensed pass/fail report. For a quick single-file check you may still call the MCP tool inline. +Dev-tooling runs on the `php-tooling` MCP server: PHPStan, ECS, PHPUnit, Rector; a mechanical fix is e.g. `ecs_fix` on the `php-tooling` MCP server. +Dev-tooling runs on the `js-admin-tooling` and `js-storefront-tooling` MCP servers: ESLint, Stylelint, Prettier, TypeScript, Jest, Vitest, ludtwig, and Vite/Webpack builds. diff --git a/plugins/dev-tooling/hooks/scripts/check-js-admin-tools.sh b/plugins/dev-tooling/hooks/scripts/check-js-admin-tools.sh index ab09384..fe342f8 100755 --- a/plugins/dev-tooling/hooks/scripts/check-js-admin-tools.sh +++ b/plugins/dev-tooling/hooks/scripts/check-js-admin-tools.sh @@ -23,20 +23,17 @@ is_admin_context() { return 0 fi # Admin-specific npm scripts - if echo "$COMMAND" | grep -qE 'npm\s+run\s+(lint:types|format|lint:twig|lint:all)(\s|$)'; then + if echo "$COMMAND" | grep -qE "${NPM_RUN}(lint:types|format|lint:twig|lint:all)${NAME_END}"; then return 0 fi # Not clearly Admin context - check if it's Storefront - if echo "$COMMAND" | grep -qiE 'Storefront|/app/storefront'; then + if echo "$COMMAND" | grep -qiE 'Storefront|/app/storefront|views/components'; then return 1 fi - if echo "$COMMAND" | grep -qE 'npm\s+run\s+(lint:js|production|development|eslint:app|eslint:components|stylelint:app)(\s|$)'; then + if echo "$COMMAND" | grep -qE "${NPM_RUN}(lint:js|production|development)${NAME_END}"; then return 1 fi # Unknown context - Admin hook handles generic commands. - # This is what claims a bare `npm run jest:base`, a script name that carries - # no side-specific marker. The Storefront hook declines an unknown context, - # so exactly one of the two hooks blocks it. return 0 } @@ -49,17 +46,22 @@ fi # ESLint - Use eslint_check or eslint_fix # ============================================================================ -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+lint(\s|--|$)'; then +if runs_npm_script 'lint'; then block_tool "js-admin-tooling" \ "Use eslint_check for linting or eslint_fix to auto-fix." "eslint_check" fi -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+lint:fix(\s|$)'; then +if runs_npm_script 'lint:fix'; then block_tool "js-admin-tooling" \ "Use eslint_fix to auto-fix ESLint violations." "eslint_fix" fi -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+lint:debugging(\s|--|$)'; then +if runs_npm_script 'lint-fix'; then + block_tool "js-admin-tooling" \ + "Use eslint_fix to auto-fix ESLint violations." "eslint_fix" +fi + +if runs_npm_script 'lint:debugging'; then block_tool "js-admin-tooling" \ "Use eslint_check with paths for linting, or eslint_fix with paths to auto-fix." "eslint_check" fi @@ -73,21 +75,16 @@ fi # Stylelint - Use stylelint_check or stylelint_fix # ============================================================================ -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+lint:scss(\s|--|$)'; then +if runs_npm_script 'lint:scss'; then block_tool "js-admin-tooling" \ "Use stylelint_check for SCSS/CSS linting." "stylelint_check" fi -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+lint:scss-fix(\s|$)'; then +if runs_npm_script 'lint:scss-fix'; then block_tool "js-admin-tooling" \ "Use stylelint_fix to auto-fix Stylelint violations." "stylelint_fix" fi -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+stylelint:base(\s|--|$)'; then - block_tool "js-admin-tooling" \ - "Use stylelint_check with paths for SCSS/CSS linting, or stylelint_fix with paths to auto-fix." "stylelint_check" -fi - if runs_binary stylelint; then block_tool "js-admin-tooling" \ "Use stylelint_check for SCSS/CSS linting or stylelint_fix to auto-fix." "stylelint_check" @@ -97,21 +94,16 @@ fi # Prettier - Use prettier_check or prettier_fix (Admin only) # ============================================================================ -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+format(\s|$)'; then +if runs_npm_script 'format'; then block_tool "js-admin-tooling" \ "Use prettier_check to verify formatting." "prettier_check" fi -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+format:fix(\s|$)'; then +if runs_npm_script 'format:fix'; then block_tool "js-admin-tooling" \ "Use prettier_fix to auto-format files." "prettier_fix" fi -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+prettier:base(\s|--|$)'; then - block_tool "js-admin-tooling" \ - "Use prettier_check with paths to verify formatting, or prettier_fix with paths to auto-format." "prettier_check" -fi - if runs_binary prettier; then block_tool "js-admin-tooling" \ "Use prettier_check to verify formatting or prettier_fix to auto-format." "prettier_check" @@ -121,26 +113,21 @@ fi # Jest - Use jest_run # ============================================================================ -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+unit(\s|--|$)'; then - block_tool "js-admin-tooling" \ - "Use jest_run with testPathPattern, testNamePattern, coverage options." "jest_run" -fi - -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+jest:base(\s|--|$)'; then +if runs_npm_script 'unit'; then block_tool "js-admin-tooling" \ - "Use jest_run with testPathPatterns, testNamePattern, coverage, ci options." "jest_run" + "Use jest_run with testPathPatterns, testNamePattern, coverage options." "jest_run" fi if runs_binary jest; then block_tool "js-admin-tooling" \ - "Use jest_run with testPathPattern, testNamePattern, coverage options." "jest_run" + "Use jest_run with testPathPatterns, testNamePattern, coverage options." "jest_run" fi # ============================================================================ # TypeScript - Use tsc_check (Admin only) # ============================================================================ -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+lint:types(\s|$)'; then +if runs_npm_script 'lint:types'; then block_tool "js-admin-tooling" \ "Use tsc_check for TypeScript type checking." "tsc_check" fi @@ -154,12 +141,12 @@ fi # Combined Lint - Use lint_all, lint_twig (Admin only) # ============================================================================ -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+lint:all(\s|$)'; then +if runs_npm_script 'lint:all'; then block_tool "js-admin-tooling" \ "Use lint_all to run all lint checks (TypeScript, ESLint, Stylelint, Prettier)." "lint_all" fi -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+lint:twig(\s|$)'; then +if runs_npm_script 'lint:twig'; then block_tool "js-admin-tooling" \ "Use lint_twig for Twig template linting." "lint_twig" fi @@ -168,7 +155,7 @@ fi # Build - Use vite_build (Admin only) # ============================================================================ -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+build(\s|--|$)'; then +if runs_npm_script 'build'; then block_tool "js-admin-tooling" \ "Use vite_build with mode (development/production) option." "vite_build" fi diff --git a/plugins/dev-tooling/hooks/scripts/check-js-storefront-tools.sh b/plugins/dev-tooling/hooks/scripts/check-js-storefront-tools.sh index ae39f9a..da0f60c 100755 --- a/plugins/dev-tooling/hooks/scripts/check-js-storefront-tools.sh +++ b/plugins/dev-tooling/hooks/scripts/check-js-storefront-tools.sh @@ -18,28 +18,25 @@ load_mcp_config "js-tooling" # Check if command is in Storefront context is_storefront_context() { - # Path-based detection (case-insensitive) - if echo "$COMMAND" | grep -qiE 'Storefront|/app/storefront'; then + # Path-based detection (case-insensitive). views/components is the + # Storefront component tree, named relative to src/Storefront/Resources. + if echo "$COMMAND" | grep -qiE 'Storefront|/app/storefront|views/components'; then return 0 fi - # Storefront-specific npm scripts. eslint:app, eslint:components and - # stylelint:app are Storefront names; without them here the Admin hook's - # unknown-context fallback would claim them and name the wrong server's - # tool. - # `:` is absent from the trailing `(\s|$)` alternation, so a bare `lint:js` - # alternative never covers the longer colon-suffixed names — lint:js:fix and - # the lint:js:app / lint:js:components pairs need the explicit suffix group - # or the whole family misses this detector. No Administration script name - # begins with `lint:js`, so the group cannot claim an Admin command. - if echo "$COMMAND" | grep -qE 'npm\s+run\s+(lint:js(:[A-Za-z]+)*|production|development|eslint:app|eslint:components|stylelint:app)(\s|$)'; then + # Storefront-specific npm scripts. + # `:` is absent from NAME_END, so a bare `lint:js` alternative never covers + # lint:js:fix; the suffix group does. No Administration script name begins + # with `lint:js`, so the group cannot claim an Admin command. + if echo "$COMMAND" | grep -qE "${NPM_RUN}(lint:js(:[A-Za-z]+)*|production|development)${NAME_END}"; then return 0 fi # Storefront component test scripts (Vitest) - if echo "$COMMAND" | grep -qE 'npm\s+run\s+unit:components(:watch|:coverage)?(\s|$)'; then + if echo "$COMMAND" | grep -qE "${NPM_RUN}unit:components(:watch|:coverage)?${NAME_END}"; then return 0 fi - # ludtwig only lints Storefront Twig templates - if echo "$COMMAND" | grep -qE '(^|;|&&|\||[[:space:]])ludtwig(\s|:|$)'; then + # ludtwig only lints Storefront Twig templates. The tail is NAME_END plus + # `:` (ludtwig:storefront), so `ludtwig;` reaches the block rows. + if echo "$COMMAND" | grep -qE '(^|;|&&|\||[[:space:]])ludtwig([[:space:];&|<>):]|$)'; then return 0 fi # Not Storefront context @@ -51,64 +48,37 @@ if ! is_storefront_context; then exit 0 fi +# Composer's script runner, with its global options and an optional `run` / +# `run-script` subcommand; COMPOSER_RUN comes from lib/js_binary.sh, shared with +# the PHP hook. +COMPOSER_SCRIPT="${COMMAND_POSITION}${COMPOSER_RUN}" + # ============================================================================ # ESLint - Use eslint_check or eslint_fix # ============================================================================ # Storefront-specific ESLint (lint:js) -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+lint:js(\s|--|$)'; then +if runs_npm_script 'lint:js'; then block_tool "js-storefront-tooling" \ "Use eslint_check for linting." "eslint_check" fi -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+lint:js:fix(\s|$)'; then +if runs_npm_script 'lint:js:fix'; then block_tool "js-storefront-tooling" \ "Use eslint_fix to auto-fix ESLint violations." "eslint_fix" fi -# lint:js and lint:js:fix each chain the two per-tree scripts below, which are -# reachable on their own too. -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+lint:js:app(\s|--|$)'; then - block_tool "js-storefront-tooling" \ - "Use eslint_check with paths for linting, or eslint_fix with paths to auto-fix." "eslint_check" -fi - -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+lint:js:components(\s|--|$)'; then - block_tool "js-storefront-tooling" \ - "Use eslint_check with paths under views/components/ for linting, or eslint_fix to auto-fix." "eslint_check" -fi - -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+lint:js:app:fix(\s|$)'; then +# The Storefront `lint` runs ESLint, then Stylelint; `lint-fix` runs both fixers. +if runs_npm_script 'lint'; then block_tool "js-storefront-tooling" \ - "Use eslint_fix with paths to auto-fix ESLint violations." "eslint_fix" + "npm run lint runs ESLint and Stylelint: use eslint_check for ESLint and stylelint_check for Stylelint." \ + "eslint_check" "stylelint_check" fi -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+lint:js:components:fix(\s|$)'; then +if runs_npm_script 'lint-fix'; then block_tool "js-storefront-tooling" \ - "Use eslint_fix with paths under views/components/ to auto-fix ESLint violations." "eslint_fix" -fi - -# Generic lint in Storefront context -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+lint(\s|--|$)'; then - block_tool "js-storefront-tooling" \ - "Use eslint_check for linting or eslint_fix to auto-fix." "eslint_check" -fi - -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+lint:fix(\s|$)'; then - block_tool "js-storefront-tooling" \ - "Use eslint_fix to auto-fix ESLint violations." "eslint_fix" -fi - -# Target-less base scripts that Shopware's Storefront package.json does not -# define and the MCP tools do not call; the redirects cover a project that does. -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+eslint:app(\s|--|$)'; then - block_tool "js-storefront-tooling" \ - "Use eslint_check with paths for linting, or eslint_fix with paths to auto-fix." "eslint_check" -fi - -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+eslint:components(\s|--|$)'; then - block_tool "js-storefront-tooling" \ - "Use eslint_check with paths under views/components/ for linting, or eslint_fix to auto-fix." "eslint_check" + "npm run lint-fix runs the ESLint and Stylelint fixers: use eslint_fix for ESLint and stylelint_fix for Stylelint." \ + "eslint_fix" "stylelint_fix" fi if runs_binary eslint; then @@ -120,21 +90,16 @@ fi # Stylelint - Use stylelint_check or stylelint_fix # ============================================================================ -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+lint:scss(\s|--|$)'; then +if runs_npm_script 'lint:scss'; then block_tool "js-storefront-tooling" \ "Use stylelint_check for SCSS/CSS linting." "stylelint_check" fi -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+lint:scss-fix(\s|$)'; then +if runs_npm_script 'lint:scss-fix'; then block_tool "js-storefront-tooling" \ "Use stylelint_fix to auto-fix Stylelint violations." "stylelint_fix" fi -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+stylelint:app(\s|--|$)'; then - block_tool "js-storefront-tooling" \ - "Use stylelint_check with paths for SCSS/CSS linting, or stylelint_fix with paths to auto-fix." "stylelint_check" -fi - if runs_binary stylelint; then block_tool "js-storefront-tooling" \ "Use stylelint_check for SCSS/CSS linting or stylelint_fix to auto-fix." "stylelint_check" @@ -144,19 +109,11 @@ fi # Jest - Use jest_run # ============================================================================ -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+unit(\s|--|$)'; then +if runs_npm_script 'unit'; then block_tool "js-storefront-tooling" \ "Use jest_run with testPathPatterns, testNamePattern, coverage options." "jest_run" fi -# jest:base carries no side-specific marker, so it reaches this block only when -# the command names the Storefront tree; a bare invocation stays with the Admin -# hook's unknown-context fallback. -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+jest:base(\s|--|$)'; then - block_tool "js-storefront-tooling" \ - "Use jest_run with testPathPatterns, testNamePattern, coverage, ci options." "jest_run" -fi - if runs_binary jest; then block_tool "js-storefront-tooling" \ "Use jest_run with testPathPatterns, testNamePattern, coverage options." "jest_run" @@ -166,7 +123,7 @@ fi # Vitest (views/components component suite) - Use vitest_run # ============================================================================ -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+unit:components(:watch|:coverage)?(\s|--|$)'; then +if runs_npm_script 'unit:components(:watch|:coverage)?'; then block_tool "js-storefront-tooling" \ "Use vitest_run with paths, testNamePattern, coverage options." "vitest_run" fi @@ -176,7 +133,7 @@ if runs_binary vitest; then "Use vitest_run with paths, testNamePattern, coverage options." "vitest_run" fi -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*composer\s+storefront:components:unit(\s|$)'; then +if echo "$COMMAND" | grep -qE "${COMPOSER_SCRIPT}storefront:components:unit${NAME_END}"; then block_tool "js-storefront-tooling" \ "Use vitest_run with paths, testNamePattern, coverage options." "vitest_run" fi @@ -185,12 +142,12 @@ fi # ludtwig - Use ludtwig_check or ludtwig_fix # ============================================================================ -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*composer\s+ludtwig:storefront:fix(\s|$)'; then +if echo "$COMMAND" | grep -qE "${COMPOSER_SCRIPT}ludtwig:storefront:fix${NAME_END}"; then block_tool "js-storefront-tooling" \ "Use ludtwig_fix to auto-fix Twig template violations." "ludtwig_fix" fi -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*composer\s+ludtwig:storefront(\s|$)'; then +if echo "$COMMAND" | grep -qE "${COMPOSER_SCRIPT}ludtwig:storefront${NAME_END}"; then block_tool "js-storefront-tooling" \ "Use ludtwig_check for Twig template linting or ludtwig_fix to auto-fix." "ludtwig_check" fi @@ -204,7 +161,7 @@ fi # optional `--`, because Composer *requires* `composer exec -- ludtwig` as soon # as ludtwig takes its own options — the likeliest real invocation is the one # with the separator, and `npx -y` / `pnpm dlx --` are the same shape. -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*ludtwig(\s|$)|(^|;|&&|\|)\s*(composer\s+exec|npm\s+(exec|x)|npx|pnpm\s+exec|pnpm\s+dlx|bunx|yarn\s+exec|yarn\s+run)(\s+(--?[A-Za-z0-9][-A-Za-z0-9]*|--))*\s+ludtwig(\s|$)'; then +if echo "$COMMAND" | grep -qE "${COMMAND_POSITION}ludtwig${NAME_END}|${COMMAND_POSITION}(composer\s+exec|npm\s+(exec|x)|npx|pnpm\s+exec|pnpm\s+dlx|bunx|yarn\s+exec|yarn\s+run)(\s+(--?[A-Za-z0-9][-A-Za-z0-9]*|--))*\s+ludtwig${NAME_END}"; then block_tool "js-storefront-tooling" \ "Use ludtwig_check for Twig template linting or ludtwig_fix to auto-fix." "ludtwig_check" fi @@ -213,7 +170,7 @@ fi # Build - Use webpack_build (Storefront only) # ============================================================================ -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*npm\s+run\s+(production|development)(\s|$)'; then +if runs_npm_script 'production|development'; then block_tool "js-storefront-tooling" \ "Use webpack_build with mode (development/production) option." "webpack_build" fi diff --git a/plugins/dev-tooling/hooks/scripts/check-php-tools.sh b/plugins/dev-tooling/hooks/scripts/check-php-tools.sh index aed8388..38d7cf3 100755 --- a/plugins/dev-tooling/hooks/scripts/check-php-tools.sh +++ b/plugins/dev-tooling/hooks/scripts/check-php-tools.sh @@ -11,20 +11,20 @@ set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "${SCRIPT_DIR}/lib/common.sh" +source "${SCRIPT_DIR}/lib/js_binary.sh" parse_hook_input load_mcp_config "php-tooling" +# COMMAND_POSITION, NAME_END and COMPOSER_RUN come from lib/js_binary.sh, shared +# with the JS hooks. +VENDOR_BIN="(php\s+)?\.?/?vendor/bin/" + # ============================================================================ # PHPStan - Use phpstan_analyze # ============================================================================ -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*(php\s+)?\.?/?vendor/bin/phpstan(\s|$)'; then - block_tool "php-tooling" \ - "Use phpstan_analyze for static analysis with configurable level (0-9) and paths." "phpstan_analyze" -fi - -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*composer\s+phpstan(\s|$)'; then +if echo "$COMMAND" | grep -qE "${COMMAND_POSITION}(${VENDOR_BIN}phpstan|${COMPOSER_RUN}phpstan)${NAME_END}"; then block_tool "php-tooling" \ "Use phpstan_analyze for static analysis with configurable level (0-9) and paths." "phpstan_analyze" fi @@ -33,26 +33,38 @@ fi # ECS / PHP-CS-Fixer - Use ecs_check or ecs_fix # ============================================================================ -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*(php\s+)?\.?/?vendor/bin/(ecs|php-cs-fixer)(\s|$)'; then +if echo "$COMMAND" | grep -qE "${COMMAND_POSITION}(${VENDOR_BIN}(ecs|php-cs-fixer))${NAME_END}"; then block_tool "php-tooling" \ "Use ecs_check for dry-run validation or ecs_fix to apply fixes." "ecs_check" "ecs_fix" fi -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*composer\s+ecs(-fix)?(\s|$)'; then +# The Composer script names say which mode runs: `ecs` is the check, `ecs-fix` +# the fixer. `cs` / `cs-fix` are Shopware core's names for the same two. +if echo "$COMMAND" | grep -qE "${COMMAND_POSITION}${COMPOSER_RUN}ecs${NAME_END}"; then block_tool "php-tooling" \ - "Use ecs_check for dry-run validation or ecs_fix to apply fixes." "ecs_check" "ecs_fix" + "Use ecs_check for dry-run validation." "ecs_check" fi -# ============================================================================ -# PHPUnit - Use phpunit_run -# ============================================================================ +if echo "$COMMAND" | grep -qE "${COMMAND_POSITION}${COMPOSER_RUN}ecs-fix${NAME_END}"; then + block_tool "php-tooling" \ + "Use ecs_fix to apply fixes." "ecs_fix" +fi -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*(php\s+)?\.?/?vendor/bin/phpunit(\s|$)'; then +if echo "$COMMAND" | grep -qE "${COMMAND_POSITION}${COMPOSER_RUN}cs${NAME_END}"; then block_tool "php-tooling" \ - "Use phpunit_run with testsuite, paths, filter, coverage, stop_on_failure options." "phpunit_run" + "Use ecs_check for dry-run validation." "ecs_check" +fi + +if echo "$COMMAND" | grep -qE "${COMMAND_POSITION}${COMPOSER_RUN}cs-fix${NAME_END}"; then + block_tool "php-tooling" \ + "Use ecs_fix to apply fixes." "ecs_fix" fi -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*composer\s+phpunit(\s|$)'; then +# ============================================================================ +# PHPUnit - Use phpunit_run +# ============================================================================ + +if echo "$COMMAND" | grep -qE "${COMMAND_POSITION}(${VENDOR_BIN}phpunit|${COMPOSER_RUN}phpunit)${NAME_END}"; then block_tool "php-tooling" \ "Use phpunit_run with testsuite, paths, filter, coverage, stop_on_failure options." "phpunit_run" fi @@ -61,7 +73,7 @@ fi # Symfony Console - Use console_run or console_list # ============================================================================ -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*(php\s+)?\.?/?bin/console(\s|$)'; then +if echo "$COMMAND" | grep -qE "${COMMAND_POSITION}(php\s+)?\.?/?bin/console${NAME_END}"; then block_tool "php-tooling" \ "Use console_run to execute commands or console_list to list available commands." "console_run" "console_list" fi @@ -70,14 +82,14 @@ fi # Rector - Use rector_fix or rector_check # ============================================================================ -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*(php\s+)?\.?/?vendor/bin/rector(\s|$)'; then +if echo "$COMMAND" | grep -qE "${COMMAND_POSITION}(${VENDOR_BIN}rector|${COMPOSER_RUN}rector)${NAME_END}"; then block_tool "php-tooling" \ "Use rector_fix to apply refactorings or rector_check for dry-run preview." "rector_fix" "rector_check" fi -if echo "$COMMAND" | grep -qE '(^|;|&&|\|)\s*composer\s+rector(-fix)?(\s|$)'; then +if echo "$COMMAND" | grep -qE "${COMMAND_POSITION}${COMPOSER_RUN}rector-fix${NAME_END}"; then block_tool "php-tooling" \ - "Use rector_fix to apply refactorings or rector_check for dry-run preview." "rector_fix" "rector_check" + "Use rector_fix to apply refactorings." "rector_fix" fi exit 0 diff --git a/plugins/dev-tooling/hooks/scripts/lib/common.sh b/plugins/dev-tooling/hooks/scripts/lib/common.sh index 565fffb..6d9d565 100644 --- a/plugins/dev-tooling/hooks/scripts/lib/common.sh +++ b/plugins/dev-tooling/hooks/scripts/lib/common.sh @@ -14,7 +14,7 @@ # Global variables set by this library: # HOOK_INPUT - The hook-event JSON read from stdin # COMMAND - The bash command being checked -# CONFIG_FILE - Path to loaded config file (or empty) +# MCP_CONFIG_JSON - The config read by read_mcp_config (or empty) # ENVIRONMENT - Environment from config (native/docker/docker-compose/vagrant/ddev) # ENFORCE_MCP_TOOLS - Whether to enforce MCP tools (true/false) @@ -42,38 +42,71 @@ parse_hook_input() { fi } -# Load MCP config from project directory (resolve_project_dir on HOOK_INPUT) +# Read the MCP config the way the MCP servers find theirs: the file named by +# MCP_PHP_TOOLING_CONFIG or MCP_JS_TOOLING_CONFIG when that variable is set, +# else every file found at the servers' locations, deep-merged with later +# locations winning. A missing override file reads as no config, so the +# defaults apply. So does any file that is empty or does not hold a JSON object: +# merging alone would drop an unreadable file and let a lower-priority file +# decide the result, so every file is checked before the merge. +# Args: $1 = config prefix ("php-tooling" or "js-tooling") +# $2 = project directory, may be empty +# Sets: MCP_CONFIG_JSON - the merged config object, or empty when none is read +read_mcp_config() { + local config_prefix="$1" project_dir="$2" + local override="" location + local -a files=() + MCP_CONFIG_JSON="" + + case "$config_prefix" in + php-tooling) override="${MCP_PHP_TOOLING_CONFIG:-}" ;; + js-tooling) override="${MCP_JS_TOOLING_CONFIG:-}" ;; + esac + + if [[ -n "$override" ]]; then + if [[ -f "$override" ]]; then + files=("$override") + fi + elif [[ -n "$project_dir" ]]; then + # Merge order of CONFIG_LOCATIONS in templates/mcp-shared/config.sh. + for location in "" .aiassistant/ .amazonq/ .cline/ .cursor/ .kiro/ .windsurf/ .zed/ .claude/; do + if [[ -f "${project_dir}/${location}.mcp-${config_prefix}.json" ]]; then + files+=("${project_dir}/${location}.mcp-${config_prefix}.json") + fi + done + fi + + if [[ ${#files[@]} -eq 0 ]]; then + return 0 + fi + + local file + for file in "${files[@]}"; do + if ! jq -e 'type == "object"' "$file" >/dev/null 2>&1; then + return 0 + fi + done + + MCP_CONFIG_JSON=$(jq -s 'reduce .[] as $config ({}; . * $config)' "${files[@]}" 2>/dev/null) || MCP_CONFIG_JSON="" +} + +# Load MCP config for the project directory (resolve_project_dir on HOOK_INPUT) # Args: $1 = config prefix (e.g., "php-tooling", "js-tooling") -# Sets: CONFIG_FILE, ENVIRONMENT, ENFORCE_MCP_TOOLS (globals) +# Sets: MCP_CONFIG_JSON, ENVIRONMENT, ENFORCE_MCP_TOOLS (globals) # Exits 0 if enforcement is disabled load_mcp_config() { local config_prefix="$1" - CONFIG_FILE="" ENVIRONMENT="" ENFORCE_MCP_TOOLS="true" - local project_dir - project_dir=$(resolve_project_dir "$HOOK_INPUT") + read_mcp_config "$config_prefix" "$(resolve_project_dir "$HOOK_INPUT")" - if [[ -n "$project_dir" ]]; then - # Check config locations in priority order - for location in ".claude/.mcp-${config_prefix}.json" ".mcp-${config_prefix}.json"; do - if [[ -f "${project_dir}/${location}" ]]; then - CONFIG_FILE="${project_dir}/${location}" - break - fi - done - - if [[ -n "$CONFIG_FILE" ]]; then - ENVIRONMENT=$(jq -r '.environment // empty' "$CONFIG_FILE" 2>/dev/null || true) - # Check if MCP tool enforcement is disabled (default: true) - # Note: jq's // operator treats false as falsy, so we check explicitly - local enforce_value - enforce_value=$(jq -r 'if .enforce_mcp_tools == false then "false" else "true" end' "$CONFIG_FILE" 2>/dev/null || echo "true") - if [[ "$enforce_value" == "false" ]]; then - ENFORCE_MCP_TOOLS="false" - fi - fi + if [[ -n "$MCP_CONFIG_JSON" ]]; then + # jq's // treats false as absent, so the opt-out is compared explicitly. + local settings + settings=$(jq -r '"\(.enforce_mcp_tools != false)\t\(.environment // "")"' <<< "$MCP_CONFIG_JSON") + ENFORCE_MCP_TOOLS="${settings%%$'\t'*}" + ENVIRONMENT="${settings#*$'\t'}" fi # Early exit if enforcement is disabled diff --git a/plugins/dev-tooling/hooks/scripts/lib/js_binary.sh b/plugins/dev-tooling/hooks/scripts/lib/js_binary.sh index cdd135b..a1ebf3a 100644 --- a/plugins/dev-tooling/hooks/scripts/lib/js_binary.sh +++ b/plugins/dev-tooling/hooks/scripts/lib/js_binary.sh @@ -1,24 +1,59 @@ #!/bin/bash -# Matches a direct run of a JS tool binary, for the Admin and Storefront JS -# hooks; each hook decides which binaries it checks. Owned by dev-tooling, not -# templated like common.sh. +# Matches a direct run of a JS tool binary or an npm script, for the Admin and +# Storefront JS hooks; each hook decides which binaries and scripts it checks. +# Owned by dev-tooling, not templated like common.sh. # -# Matched at a command position: `npx`, `npm exec` or `npm x` followed by flags -# that take no value and an optional `--`; a node_modules/.bin path; and the -# -c / --call string of those runners, at its start or after `;`, `&&` or `|`. +# Public: +# COMMAND_POSITION ERE prefix: start of command or after `;`, `&&`, `||` +# or `|`, then any number of `NAME=value` assignments +# NAME_END ERE suffix: what may follow an exact binary or script name +# NPM_RUN npm with its flags, `run` or `run-script`, and flags +# COMPOSER_RUN composer with its global options and `run` or `run-script` +# runs_binary COMMAND runs the binary +# runs_npm_script +# COMMAND runs the npm script whose whole name matches +# +# A binary is matched at a command position: `npx`, `npm exec` or `npm x` +# followed by flags that take no value and an optional `--`; a node_modules/.bin +# path, optionally run through `node`; and the -c / --call string of those +# runners, at its start or after `;`, `&&` or `|`. # A bare binary name is not matched: it also sits at a command position inside -# quoted text such as a commit message, which would then be blocked. +# quoted text such as a commit message, which would then be blocked. For the +# same reason COMMAND_POSITION has no `(`: `-m "fix (npx eslint)"` is text. # A flag that takes a value is not skipped, because its value would read as the # binary; before -c it is, because there the string names what runs. -BINARY_INVOCATION='(^|;|&&|\|)\s*((npm\s+(exec|x)|npx)(\s+(-y|--yes|--no|--no-install|-q|--quiet|--silent|--))*\s+|\S*node_modules/\.bin/)' +COMMAND_POSITION='(^|;|&&|\|)\s*([A-Za-z_][A-Za-z0-9_]*=\S*\s+)*' + +# `:` and `-` are absent, so `lint` never matches `lint:fix`, `lint--x` or `lintx`. +NAME_END='([[:space:];&|<>)]|$)' + +BINARY_INVOCATION="${COMMAND_POSITION}((npm\s+(exec|x)|npx)(\s+(-y|--yes|--no|--no-install|-q|--quiet|--silent|--))*\s+|(node\s+)?\S*node_modules/\.bin/)" + +CALL_INVOCATION="${COMMAND_POSITION}(npm\s+(exec|x)|npx)(\s+(-y|--yes|--no|--no-install|-q|--quiet|--silent|(--package|--workspace|-p|-w)(=|\s+)[^[:space:]\"';&|]+))*\s+(-c|--call)(\s+|=)[\"']?\s*([^\"';&|]*(;|&&|\|)\s*)*([A-Za-z_][A-Za-z0-9_]*=[^[:space:]\"';&|]*\s+)*" -CALL_INVOCATION="(^|;|&&|\|)\s*(npm\s+(exec|x)|npx)(\s+(-y|--yes|--no|--no-install|-q|--quiet|--silent|(--package|--workspace|-p|-w)(=|\s+)[^[:space:]\"';&|]+))*\s+(-c|--call)(\s+|=)[\"']?\s*([^\"';&|]*(;|&&|\|)\s*)*([A-Za-z_][A-Za-z0-9_]*=[^[:space:]\"';&|]*\s+)*" +# npm, its flags (`--prefix` with its value), `run` or `run-script`, then flags +# before the script name. The detectors in the hooks use it unanchored. +NPM_RUN='npm(\s+(--?[A-Za-z][-A-Za-z]*|--prefix\s+\S+))*\s+run(-script)?(\s+--?[A-Za-z][-A-Za-z]*)*\s+' + +# Composer's script runner: composer, then its global options (`-d` and +# `--working-dir` with their value, and any other flag that takes none), then an +# optional `run` or `run-script` before the script name. Each hook prefixes it +# with COMMAND_POSITION where it needs the match anchored to a command. +# shellcheck disable=SC2034 # read by check-php-tools.sh when it sources this file +COMPOSER_RUN='composer\s+((-d|--working-dir)(=|\s+)?\S+\s+|-\S+\s+)*(run(-script)?\s+)?' # Globals: reads COMMAND, set by parse_hook_input in common.sh # Args: $1 = binary name # Returns: 0 when COMMAND runs the binary in one of the forms above runs_binary() { - echo "$COMMAND" | grep -qE "${BINARY_INVOCATION}$1(@\S+)?(\s|\$)" \ - || echo "$COMMAND" | grep -qE "${CALL_INVOCATION}$1(@\S+)?(\s|\$|[\"'])" + echo "$COMMAND" | grep -qE "${BINARY_INVOCATION}$1(@\S+)?${NAME_END}" \ + || echo "$COMMAND" | grep -qE "${CALL_INVOCATION}$1(@\S+)?([[:space:];&|<>)\"']|\$)" +} + +# Globals: reads COMMAND, set by parse_hook_input in common.sh +# Args: $1 = ERE matching the whole script name, e.g. 'lint' or 'unit:components(:watch)?' +# Returns: 0 when COMMAND runs that npm script at a command position +runs_npm_script() { + echo "$COMMAND" | grep -qE "${COMMAND_POSITION}${NPM_RUN}($1)${NAME_END}" } diff --git a/plugins/dev-tooling/hooks/scripts/session-start.sh b/plugins/dev-tooling/hooks/scripts/session-start.sh index ae80aa7..3c4366a 100755 --- a/plugins/dev-tooling/hooks/scripts/session-start.sh +++ b/plugins/dev-tooling/hooks/scripts/session-start.sh @@ -23,20 +23,10 @@ HOOK_INPUT=$(cat) PROJECT_DIR=$(resolve_project_dir "$HOOK_INPUT") || PROJECT_DIR="" is_enforced() { - local config_prefix="$1" - [[ -z "${PROJECT_DIR}" ]] && return 0 - local config_file="" - for location in ".claude/.mcp-${config_prefix}.json" ".mcp-${config_prefix}.json"; do - if [[ -f "${PROJECT_DIR}/${location}" ]]; then - config_file="${PROJECT_DIR}/${location}" - break - fi - done - [[ -z "$config_file" ]] && return 0 command -v jq &>/dev/null || return 0 - local val - val=$(jq -r 'if .enforce_mcp_tools == false then "false" else "true" end' "$config_file" 2>/dev/null || echo "true") - [[ "$val" == "true" ]] + read_mcp_config "$1" "${PROJECT_DIR}" + [[ -z "${MCP_CONFIG_JSON}" ]] && return 0 + [[ $(jq -r '.enforce_mcp_tools != false' <<< "${MCP_CONFIG_JSON}") == "true" ]] } # _render_scopes_section @@ -44,25 +34,17 @@ is_enforced() { # no scopes are present. _render_scopes_section() { local prefix="$1" - [[ -z "${PROJECT_DIR}" ]] && return 0 command -v jq &>/dev/null || return 0 - - local config_file="" - for location in ".claude/.mcp-${prefix}.json" ".mcp-${prefix}.json"; do - if [[ -f "${PROJECT_DIR}/${location}" ]]; then - config_file="${PROJECT_DIR}/${location}" - break - fi - done - [[ -z "${config_file}" ]] && return 0 + read_mcp_config "${prefix}" "${PROJECT_DIR}" + [[ -z "${MCP_CONFIG_JSON}" ]] && return 0 local has_scopes - has_scopes=$(jq -r '.scopes // {} | keys | length' "${config_file}" 2>/dev/null || echo "0") + has_scopes=$(jq -r '.scopes // {} | keys | length' <<< "${MCP_CONFIG_JSON}" 2>/dev/null || echo "0") [[ "${has_scopes}" -eq 0 ]] && return 0 local default_scope names - default_scope=$(jq -r '.default_scope // "shopware"' "${config_file}") - names=$(jq -r '.scopes | keys | join(", ")' "${config_file}") + default_scope=$(jq -r '.default_scope // "shopware"' <<< "${MCP_CONFIG_JSON}") + names=$(jq -r '.scopes | keys | join(", ")' <<< "${MCP_CONFIG_JSON}") local ignore_note="" if [[ "${prefix}" == "js-tooling" ]]; then @@ -93,24 +75,49 @@ if [[ -z "${CLAUDE_PROJECT_DIR:-}" && -n "${PROJECT_DIR}" ]]; then "If a tool of the \`php-tooling\`, \`js-admin-tooling\` or \`js-storefront-tooling\` MCP server reports that no project root is set, call \`set_project_root\` on that server with \`${PROJECT_DIR}\` and retry.") fi -enforced=1 -is_enforced "php-tooling" || is_enforced "js-tooling" || enforced=0 -[[ "${enforced}" -eq 0 && -z "${bind_hint}" ]] && exit 0 +php_enforced=0 +js_enforced=0 +is_enforced "php-tooling" && php_enforced=1 +is_enforced "js-tooling" && js_enforced=1 +enforced=$(( php_enforced || js_enforced )) + +# Scopes are reported for every config that declares them, whatever +# enforce_mcp_tools says: the flag controls the directives that steer to the +# tools, not the scope metadata those tools accept. +scopes_block="" +scopes_block+=$(_render_scopes_section "php-tooling") +scopes_block+=$(_render_scopes_section "js-tooling") + +[[ "${enforced}" -eq 0 && -z "${bind_hint}" && -z "${scopes_block}" ]] && exit 0 + +# _render_directives +# Drops every line naming an opted-out language's server, so the prompt never directs to tools not enforced. +_render_directives() { + local languages="PHP and JavaScript" + [[ "${js_enforced}" -eq 0 ]] && languages="PHP" + [[ "${php_enforced}" -eq 0 ]] && languages="JavaScript" + awk -v php="${php_enforced}" -v js="${js_enforced}" -v languages="${languages}" ' + NR == 1 { sub(/PHP and JavaScript/, languages) } + /`php-tooling`/ && php == 0 { next } + /`js-(admin|storefront)-tooling`/ && js == 0 { next } + /^$/ { blank = 1; next } + { + if (blank && printed) print "" + blank = 0; printed = 1 + print + } + ' "$1" +} context="" if [[ "${enforced}" -eq 1 ]]; then if [[ -f "$PROMPT_FILE" ]]; then - context=$(cat "$PROMPT_FILE") + context=$(_render_directives "$PROMPT_FILE") fi +fi - # Append scopes sections (one per config prefix that declares scopes). - scopes_block="" - for prefix in php-tooling js-tooling; do - section=$(_render_scopes_section "${prefix}") - [[ -n "${section}" ]] && scopes_block+="${section}" - done - - [[ -n "${scopes_block}" ]] && context+="${scopes_block}" +if [[ -n "${scopes_block}" ]]; then + context+="${scopes_block}" fi if [[ -n "${bind_hint}" ]]; then diff --git a/plugins/shopware-env/hooks/scripts/lib/common.sh b/plugins/shopware-env/hooks/scripts/lib/common.sh index 565fffb..6d9d565 100644 --- a/plugins/shopware-env/hooks/scripts/lib/common.sh +++ b/plugins/shopware-env/hooks/scripts/lib/common.sh @@ -14,7 +14,7 @@ # Global variables set by this library: # HOOK_INPUT - The hook-event JSON read from stdin # COMMAND - The bash command being checked -# CONFIG_FILE - Path to loaded config file (or empty) +# MCP_CONFIG_JSON - The config read by read_mcp_config (or empty) # ENVIRONMENT - Environment from config (native/docker/docker-compose/vagrant/ddev) # ENFORCE_MCP_TOOLS - Whether to enforce MCP tools (true/false) @@ -42,38 +42,71 @@ parse_hook_input() { fi } -# Load MCP config from project directory (resolve_project_dir on HOOK_INPUT) +# Read the MCP config the way the MCP servers find theirs: the file named by +# MCP_PHP_TOOLING_CONFIG or MCP_JS_TOOLING_CONFIG when that variable is set, +# else every file found at the servers' locations, deep-merged with later +# locations winning. A missing override file reads as no config, so the +# defaults apply. So does any file that is empty or does not hold a JSON object: +# merging alone would drop an unreadable file and let a lower-priority file +# decide the result, so every file is checked before the merge. +# Args: $1 = config prefix ("php-tooling" or "js-tooling") +# $2 = project directory, may be empty +# Sets: MCP_CONFIG_JSON - the merged config object, or empty when none is read +read_mcp_config() { + local config_prefix="$1" project_dir="$2" + local override="" location + local -a files=() + MCP_CONFIG_JSON="" + + case "$config_prefix" in + php-tooling) override="${MCP_PHP_TOOLING_CONFIG:-}" ;; + js-tooling) override="${MCP_JS_TOOLING_CONFIG:-}" ;; + esac + + if [[ -n "$override" ]]; then + if [[ -f "$override" ]]; then + files=("$override") + fi + elif [[ -n "$project_dir" ]]; then + # Merge order of CONFIG_LOCATIONS in templates/mcp-shared/config.sh. + for location in "" .aiassistant/ .amazonq/ .cline/ .cursor/ .kiro/ .windsurf/ .zed/ .claude/; do + if [[ -f "${project_dir}/${location}.mcp-${config_prefix}.json" ]]; then + files+=("${project_dir}/${location}.mcp-${config_prefix}.json") + fi + done + fi + + if [[ ${#files[@]} -eq 0 ]]; then + return 0 + fi + + local file + for file in "${files[@]}"; do + if ! jq -e 'type == "object"' "$file" >/dev/null 2>&1; then + return 0 + fi + done + + MCP_CONFIG_JSON=$(jq -s 'reduce .[] as $config ({}; . * $config)' "${files[@]}" 2>/dev/null) || MCP_CONFIG_JSON="" +} + +# Load MCP config for the project directory (resolve_project_dir on HOOK_INPUT) # Args: $1 = config prefix (e.g., "php-tooling", "js-tooling") -# Sets: CONFIG_FILE, ENVIRONMENT, ENFORCE_MCP_TOOLS (globals) +# Sets: MCP_CONFIG_JSON, ENVIRONMENT, ENFORCE_MCP_TOOLS (globals) # Exits 0 if enforcement is disabled load_mcp_config() { local config_prefix="$1" - CONFIG_FILE="" ENVIRONMENT="" ENFORCE_MCP_TOOLS="true" - local project_dir - project_dir=$(resolve_project_dir "$HOOK_INPUT") + read_mcp_config "$config_prefix" "$(resolve_project_dir "$HOOK_INPUT")" - if [[ -n "$project_dir" ]]; then - # Check config locations in priority order - for location in ".claude/.mcp-${config_prefix}.json" ".mcp-${config_prefix}.json"; do - if [[ -f "${project_dir}/${location}" ]]; then - CONFIG_FILE="${project_dir}/${location}" - break - fi - done - - if [[ -n "$CONFIG_FILE" ]]; then - ENVIRONMENT=$(jq -r '.environment // empty' "$CONFIG_FILE" 2>/dev/null || true) - # Check if MCP tool enforcement is disabled (default: true) - # Note: jq's // operator treats false as falsy, so we check explicitly - local enforce_value - enforce_value=$(jq -r 'if .enforce_mcp_tools == false then "false" else "true" end' "$CONFIG_FILE" 2>/dev/null || echo "true") - if [[ "$enforce_value" == "false" ]]; then - ENFORCE_MCP_TOOLS="false" - fi - fi + if [[ -n "$MCP_CONFIG_JSON" ]]; then + # jq's // treats false as absent, so the opt-out is compared explicitly. + local settings + settings=$(jq -r '"\(.enforce_mcp_tools != false)\t\(.environment // "")"' <<< "$MCP_CONFIG_JSON") + ENFORCE_MCP_TOOLS="${settings%%$'\t'*}" + ENVIRONMENT="${settings#*$'\t'}" fi # Early exit if enforcement is disabled diff --git a/templates/hooks-shared/common.sh b/templates/hooks-shared/common.sh index 565fffb..6d9d565 100644 --- a/templates/hooks-shared/common.sh +++ b/templates/hooks-shared/common.sh @@ -14,7 +14,7 @@ # Global variables set by this library: # HOOK_INPUT - The hook-event JSON read from stdin # COMMAND - The bash command being checked -# CONFIG_FILE - Path to loaded config file (or empty) +# MCP_CONFIG_JSON - The config read by read_mcp_config (or empty) # ENVIRONMENT - Environment from config (native/docker/docker-compose/vagrant/ddev) # ENFORCE_MCP_TOOLS - Whether to enforce MCP tools (true/false) @@ -42,38 +42,71 @@ parse_hook_input() { fi } -# Load MCP config from project directory (resolve_project_dir on HOOK_INPUT) +# Read the MCP config the way the MCP servers find theirs: the file named by +# MCP_PHP_TOOLING_CONFIG or MCP_JS_TOOLING_CONFIG when that variable is set, +# else every file found at the servers' locations, deep-merged with later +# locations winning. A missing override file reads as no config, so the +# defaults apply. So does any file that is empty or does not hold a JSON object: +# merging alone would drop an unreadable file and let a lower-priority file +# decide the result, so every file is checked before the merge. +# Args: $1 = config prefix ("php-tooling" or "js-tooling") +# $2 = project directory, may be empty +# Sets: MCP_CONFIG_JSON - the merged config object, or empty when none is read +read_mcp_config() { + local config_prefix="$1" project_dir="$2" + local override="" location + local -a files=() + MCP_CONFIG_JSON="" + + case "$config_prefix" in + php-tooling) override="${MCP_PHP_TOOLING_CONFIG:-}" ;; + js-tooling) override="${MCP_JS_TOOLING_CONFIG:-}" ;; + esac + + if [[ -n "$override" ]]; then + if [[ -f "$override" ]]; then + files=("$override") + fi + elif [[ -n "$project_dir" ]]; then + # Merge order of CONFIG_LOCATIONS in templates/mcp-shared/config.sh. + for location in "" .aiassistant/ .amazonq/ .cline/ .cursor/ .kiro/ .windsurf/ .zed/ .claude/; do + if [[ -f "${project_dir}/${location}.mcp-${config_prefix}.json" ]]; then + files+=("${project_dir}/${location}.mcp-${config_prefix}.json") + fi + done + fi + + if [[ ${#files[@]} -eq 0 ]]; then + return 0 + fi + + local file + for file in "${files[@]}"; do + if ! jq -e 'type == "object"' "$file" >/dev/null 2>&1; then + return 0 + fi + done + + MCP_CONFIG_JSON=$(jq -s 'reduce .[] as $config ({}; . * $config)' "${files[@]}" 2>/dev/null) || MCP_CONFIG_JSON="" +} + +# Load MCP config for the project directory (resolve_project_dir on HOOK_INPUT) # Args: $1 = config prefix (e.g., "php-tooling", "js-tooling") -# Sets: CONFIG_FILE, ENVIRONMENT, ENFORCE_MCP_TOOLS (globals) +# Sets: MCP_CONFIG_JSON, ENVIRONMENT, ENFORCE_MCP_TOOLS (globals) # Exits 0 if enforcement is disabled load_mcp_config() { local config_prefix="$1" - CONFIG_FILE="" ENVIRONMENT="" ENFORCE_MCP_TOOLS="true" - local project_dir - project_dir=$(resolve_project_dir "$HOOK_INPUT") + read_mcp_config "$config_prefix" "$(resolve_project_dir "$HOOK_INPUT")" - if [[ -n "$project_dir" ]]; then - # Check config locations in priority order - for location in ".claude/.mcp-${config_prefix}.json" ".mcp-${config_prefix}.json"; do - if [[ -f "${project_dir}/${location}" ]]; then - CONFIG_FILE="${project_dir}/${location}" - break - fi - done - - if [[ -n "$CONFIG_FILE" ]]; then - ENVIRONMENT=$(jq -r '.environment // empty' "$CONFIG_FILE" 2>/dev/null || true) - # Check if MCP tool enforcement is disabled (default: true) - # Note: jq's // operator treats false as falsy, so we check explicitly - local enforce_value - enforce_value=$(jq -r 'if .enforce_mcp_tools == false then "false" else "true" end' "$CONFIG_FILE" 2>/dev/null || echo "true") - if [[ "$enforce_value" == "false" ]]; then - ENFORCE_MCP_TOOLS="false" - fi - fi + if [[ -n "$MCP_CONFIG_JSON" ]]; then + # jq's // treats false as absent, so the opt-out is compared explicitly. + local settings + settings=$(jq -r '"\(.enforce_mcp_tools != false)\t\(.environment // "")"' <<< "$MCP_CONFIG_JSON") + ENFORCE_MCP_TOOLS="${settings%%$'\t'*}" + ENVIRONMENT="${settings#*$'\t'}" fi # Early exit if enforcement is disabled