Skip to content

Commit d3d8e6f

Browse files
authored
fix: scope dev server metadata to Ruby mode (#632) (#633)
Keep development and test servers from sharing a metadata file, and verify the recorded port before proxying. Require the corresponding vite-plugin-ruby patch so the Ruby reader and Vite writer use the same path.
1 parent 76fc36b commit d3d8e6f

12 files changed

Lines changed: 111 additions & 22 deletions

File tree

‎docs/src/config/index.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,10 @@ You can customize this behavior using the following options.
274274
- **Default:** `false`
275275
- **Env Var:** `VITE_RUBY_DEV_SERVER_CONNECTION_CHECK`
276276
277-
By default the dev server is detected by reading `tmp/vite-ruby.json`, a
278-
metadata file the Vite plugin writes with the dev server URL when it starts,
279-
and removes when it stops.
277+
By default the dev server is detected by reading `tmp/vite-ruby-<mode>.json`
278+
(for example, `tmp/vite-ruby-development.json`), a metadata file the Vite
279+
plugin writes when the dev server starts and removes when it stops. The
280+
metadata must have the configured port for the current mode.
280281
281282
Enable this to detect the dev server by opening a TCP connection to the
282283
configured `host` and `port` instead. Useful if the metadata file can become

‎docs/src/guide/troubleshooting.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ First, verify that the dev server is reachable by starting a new console session
166166
> ViteRuby.instance.dev_server_running?
167167
```
168168

169-
By default this reads `tmp/vite-ruby.json`, written by the Vite plugin while the dev server runs.
170-
If it returns `false` while the dev server is running, make sure you are on a recent [vite-plugin-ruby] that writes this file, and that `tmp/` is writable.
169+
By default this reads `tmp/vite-ruby-<mode>.json` (for example, `tmp/vite-ruby-development.json`), written by the Vite plugin while the server runs.
170+
If it returns `false` while the dev server is running, make sure you are using vite-plugin-ruby 5.2.5 or later, that `tmp/` is writable, and that the dev server's port matches the configured port for this mode.
171171

172172
If you have enabled <kbd>[devServerConnectionCheck]</kbd>, try increasing the <kbd>[devServerConnectTimeout]</kbd>, restart the console and retry.
173173
In systems with constrained resources the [default timeout][devServerConnectTimeout] might not be enough.

‎test/compatibility_check_test.rb‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ def test_compatible_plugin
2323
assert compatible_plugin?(nil, nil)
2424
end
2525

26+
def test_requires_plugin_version_with_mode_specific_metadata
27+
assert_equal "^5.2.5", ViteRuby::DEFAULT_PLUGIN_VERSION
28+
refute compatible_plugin?("^5.2.4", ViteRuby::DEFAULT_PLUGIN_VERSION)
29+
assert compatible_plugin?("^5.2.5", ViteRuby::DEFAULT_PLUGIN_VERSION)
30+
end
31+
2632
def test_raise_unless_satisfied
2733
assert_raises(ArgumentError) { raise_unless_satisfied("^4.1.0", "^3.0") }
2834
raise_unless_satisfied("3.1.0", "^3.0")

‎test/config_test.rb‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ def test_ruby_config_file
6363
assert_equal Gem.loaded_specs["rails"].full_gem_path, ViteRuby.config.to_env["EXAMPLE_PATH"]
6464
end
6565

66+
def test_dev_server_meta_path_is_scoped_to_mode
67+
assert_path "test_app/tmp/vite-ruby-production.json", @config.dev_server_meta_path
68+
assert_path "test_app/tmp/vite-ruby-development.json", resolve_config(mode: "development").dev_server_meta_path
69+
assert_path "test_app/tmp/vite-ruby-test.json", resolve_config(mode: "test").dev_server_meta_path
70+
end
71+
6672
def test_manifest_path
6773
assert_path "test_app/public/vite-production/.vite/manifest.json", @config.manifest_paths.first
6874
end

‎test/dev_server_test.rb‎

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,59 @@ def test_not_running_without_meta_file
1212

1313
def test_running_with_meta_file
1414
refresh_config(mode: "development")
15-
write_meta_file(url: "http://localhost:3036", host: "localhost", port: 3036, https: false, pid: 1234)
15+
write_meta_file(url: "http://localhost:3535", host: "localhost", port: ViteRuby.config.port, https: false, pid: 1234)
1616

1717
assert_predicate ViteRuby.instance, :dev_server_running?
18-
assert_equal "http://localhost:3036", ViteRuby.instance.send(:dev_server_meta)["url"]
18+
assert_equal "http://localhost:3535", ViteRuby.instance.send(:dev_server_meta)["url"]
19+
ensure
20+
remove_meta_file
21+
end
22+
23+
def test_development_meta_does_not_mark_test_server_as_running
24+
refresh_config(mode: "test")
25+
remove_meta_file
26+
test_port = ViteRuby.config.port
27+
refresh_config(mode: "development", port: test_port)
28+
write_meta_file(port: test_port)
29+
30+
refresh_config(mode: "test")
31+
32+
ENV.stub(:[], ->(key) { ENV.fetch(key, nil) unless key == "CI" }) do
33+
refute_predicate ViteRuby.instance, :dev_server_running?
34+
end
35+
ensure
36+
remove_meta_file if ViteRuby.config.mode == "test"
37+
refresh_config(mode: "development")
38+
remove_meta_file
39+
end
40+
41+
def test_running_with_test_meta_file
42+
refresh_config(mode: "test")
43+
write_meta_file(port: ViteRuby.config.port)
44+
45+
ENV.stub(:[], ->(key) { ENV.fetch(key, nil) unless key == "CI" }) do
46+
assert_predicate ViteRuby.instance, :dev_server_running?
47+
end
48+
ensure
49+
remove_meta_file
50+
end
51+
52+
def test_not_running_when_meta_port_does_not_match
53+
refresh_config(mode: "development")
54+
write_meta_file(port: ViteRuby.config.port + 1)
55+
56+
refute_predicate ViteRuby.instance, :dev_server_running?
57+
ensure
58+
remove_meta_file
59+
end
60+
61+
def test_not_running_with_invalid_meta_shape
62+
refresh_config(mode: "development")
63+
path = ViteRuby.config.dev_server_meta_path
64+
path.dirname.mkpath
65+
path.write(JSON.generate("not a metadata object"))
66+
67+
refute_predicate ViteRuby.instance, :dev_server_running?
1968
ensure
2069
remove_meta_file
2170
end

‎vite-plugin-ruby/src/constants.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ export const ENV_PREFIX = 'VITE_RUBY'
77
// Internal: Key of the vite.json file that is applied to all environments.
88
export const ALL_ENVS_KEY = 'all'
99

10-
// Internal: Path, relative to the project root, of the dev server metadata file.
11-
export const DEV_SERVER_META_FILE = 'tmp/vite-ruby.json'
12-
1310
// Internal: Extensions of CSS files or known precompilers.
1411
export const KNOWN_CSS_EXTENSIONS = [
1512
'css',

‎vite-plugin-ruby/src/dev-server.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { mkdirSync, readFileSync, rmSync, writeFileSync } from 'fs'
2-
import { dirname } from 'path'
2+
import { dirname, resolve } from 'path'
33
import type { AddressInfo } from 'net'
44
import type { ResolvedConfig } from 'vite'
55

@@ -18,6 +18,11 @@ const WILDCARD_HOSTS = new Set(['', '0.0.0.0', '::', '::1'])
1818
let exitHandlersBound = false
1919
let ownedMetaPath: string | null = null
2020

21+
// Internal: Keeps metadata for each Ruby mode separate.
22+
export function devServerMetaPath (root: string, mode: string): string {
23+
return resolve(root, 'tmp', `vite-ruby-${mode}.json`)
24+
}
25+
2126
// Internal: Returns true when the address is a resolved TCP address.
2227
function isAddressInfo (address: string | AddressInfo | null | undefined): address is AddressInfo {
2328
return Boolean(address) && typeof address === 'object'

‎vite-plugin-ruby/src/index.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import type { ConfigEnv, PluginOption, UserConfig, ViteDevServer } from 'vite'
44
import { createDebug } from 'obug'
55

66
import { cleanConfig, configOptionFromEnv } from './utils'
7+
import type { UnifiedConfig } from './types'
78
import { filterEntrypointsForRollup, loadConfiguration, resolveGlobs } from './config'
89
import { assetsManifestPlugin } from './manifest'
9-
import { bindDevServerCleanup, resolveDevServerMeta, writeDevServerMeta } from './dev-server'
10-
import { DEV_SERVER_META_FILE } from './constants'
10+
import { bindDevServerCleanup, devServerMetaPath, resolveDevServerMeta, writeDevServerMeta } from './dev-server'
1111

1212
export * from './types'
1313

@@ -105,10 +105,11 @@ function config (userConfig: UserConfig, env: ConfigEnv): UserConfig {
105105
function configureServer (server: ViteDevServer) {
106106
server.watcher.add(watchAdditionalPaths)
107107

108-
const devServerMetaPath = resolve(projectRoot, DEV_SERVER_META_FILE)
108+
const rubyConfig = server.config as typeof server.config & { viteRuby: UnifiedConfig }
109+
const metaPath = devServerMetaPath(projectRoot, rubyConfig.viteRuby.mode)
109110
server.httpServer?.once('listening', () => {
110-
writeDevServerMeta(devServerMetaPath, resolveDevServerMeta(server.httpServer?.address(), server.config))
111-
bindDevServerCleanup(devServerMetaPath)
111+
writeDevServerMeta(metaPath, resolveDevServerMeta(server.httpServer?.address(), server.config))
112+
bindDevServerCleanup(metaPath)
112113
})
113114

114115
return () => server.middlewares.use((req, res, next) => {

‎vite-plugin-ruby/tests/dev-server.spec.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { tmpdir } from 'os'
33
import { join } from 'path'
44
import { describe, it, expect } from 'vitest'
55

6-
import { removeOwnedMeta, resolveDevServerMeta, writeDevServerMeta } from '../src/dev-server'
6+
import { devServerMetaPath, removeOwnedMeta, resolveDevServerMeta, writeDevServerMeta } from '../src/dev-server'
77

88
const withServer = (server: Record<string, unknown>) => ({ server }) as any
99

@@ -23,6 +23,15 @@ describe('resolveDevServerMeta', () => {
2323
})
2424
})
2525

26+
describe('devServerMetaPath', () => {
27+
it('uses separate metadata files for the resolved Ruby modes', () => {
28+
const root = join(tmpdir(), 'vite-app')
29+
30+
expect(devServerMetaPath(root, 'development')).toBe(join(root, 'tmp/vite-ruby-development.json'))
31+
expect(devServerMetaPath(root, 'test')).toBe(join(root, 'tmp/vite-ruby-test.json'))
32+
})
33+
})
34+
2635
describe('removeOwnedMeta', () => {
2736
it('only removes the file when the pid matches', () => {
2837
const path = join(mkdtempSync(join(tmpdir(), 'vpr-')), 'vite-ruby.json')
@@ -34,4 +43,18 @@ describe('removeOwnedMeta', () => {
3443
removeOwnedMeta(path, 4242)
3544
expect(existsSync(path)).toBe(false)
3645
})
46+
47+
it('does not remove another mode\'s metadata when a server stops', () => {
48+
const root = mkdtempSync(join(tmpdir(), 'vpr-'))
49+
const devPath = devServerMetaPath(root, 'development')
50+
const testPath = devServerMetaPath(root, 'test')
51+
writeDevServerMeta(devPath, { url: 'x', host: 'h', port: 1, https: false, pid: 4242 })
52+
writeDevServerMeta(testPath, { url: 'x', host: 'h', port: 2, https: false, pid: 4243 })
53+
54+
removeOwnedMeta(devPath, 4242)
55+
56+
expect(existsSync(devPath)).toBe(false)
57+
expect(existsSync(testPath)).toBe(true)
58+
removeOwnedMeta(testPath, 4243)
59+
})
3760
})

‎vite_ruby/lib/vite_ruby.rb‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ def dev_server_running?
8787
return false unless dev_mode?
8888
return dev_server_connected? if config.dev_server_connection_check
8989

90-
!dev_server_meta.nil?
90+
meta = dev_server_meta
91+
meta.is_a?(Hash) && meta["port"] == config.port
9192
end
9293

9394
# Public: Additional environment variables to pass to Vite.

0 commit comments

Comments
 (0)