Skip to content

Commit 2adb13d

Browse files
committed
fix: prevent Sentry initialization during test execution
Resolves CI test failures caused by 'module is already linked' errors when running tests with the updated Sentry v10.5.0 dependency. * Add test environment detection in sentry.ts and server.ts * Skip Sentry.init() and tag setting during test execution * Return base MCP server without Sentry wrapping in test mode * Maintain full Sentry functionality in production environments This ensures test isolation while preserving MCP instrumentation capabilities for production deployments.
1 parent d9554c3 commit 2adb13d

2 files changed

Lines changed: 68 additions & 49 deletions

File tree

src/server/server.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,22 @@ export function createServer(): McpServer {
4343
},
4444
);
4545

46-
// Wrap server with Sentry for MCP instrumentation
47-
const server = Sentry.wrapMcpServerWithSentry(baseServer);
46+
// Check if we're in a test environment to avoid Sentry module conflicts
47+
const isTestEnvironment =
48+
process.env.NODE_ENV === 'test' ||
49+
process.env.VITEST === 'true' ||
50+
(typeof global !== 'undefined' && 'vitest' in global);
4851

49-
// Log server initialization
50-
log('info', `Server initialized with Sentry MCP instrumentation (version ${version})`);
51-
52-
return server;
52+
if (isTestEnvironment) {
53+
// In test mode, return the base server without Sentry wrapping
54+
log('info', `Server initialized without Sentry (test mode) (version ${version})`);
55+
return baseServer;
56+
} else {
57+
// In production mode, wrap server with Sentry for MCP instrumentation
58+
const server = Sentry.wrapMcpServerWithSentry(baseServer);
59+
log('info', `Server initialized with Sentry MCP instrumentation (version ${version})`);
60+
return server;
61+
}
5362
}
5463

5564
/**

src/utils/sentry.ts

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -77,51 +77,61 @@ function checkBinaryAvailability(binary: string): { available: boolean; version?
7777
return { available: true, version };
7878
}
7979

80-
Sentry.init({
81-
dsn: 'https://798607831167c7b9fe2f2912f5d3c665@o4509258288332800.ingest.de.sentry.io/4509258293837904',
82-
83-
// Setting this option to true will send default PII data to Sentry
84-
// For example, automatic IP address collection on events
85-
sendDefaultPii: true,
86-
87-
// Set release version to match application version
88-
release: `xcodebuildmcp@${version}`,
89-
90-
// Set environment based on NODE_ENV
91-
environment: process.env.NODE_ENV ?? 'development',
92-
93-
// Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring
94-
// We recommend adjusting this value in production
95-
tracesSampleRate: 1.0,
96-
});
97-
98-
// Add additional context that might be helpful for debugging
99-
const tags: Record<string, string> = {
100-
nodeVersion: process.version,
101-
platform: process.platform,
102-
arch: process.arch,
103-
};
104-
105-
// Only add Xcode Info if it's available
106-
const xcodeInfo = getXcodeInfo();
107-
if (!xcodeInfo.error) {
108-
tags.xcodeVersion = xcodeInfo.version;
109-
tags.xcodePath = xcodeInfo.path;
110-
} else {
111-
tags.xcodeVersion = 'Unknown';
112-
tags.xcodePath = 'Unknown';
80+
// Skip Sentry initialization during tests to prevent module loading conflicts
81+
const isTestEnvironment =
82+
process.env.NODE_ENV === 'test' ||
83+
process.env.VITEST === 'true' ||
84+
(typeof global !== 'undefined' && 'vitest' in global);
85+
86+
if (!isTestEnvironment) {
87+
Sentry.init({
88+
dsn: 'https://798607831167c7b9fe2f2912f5d3c665@o4509258288332800.ingest.de.sentry.io/4509258293837904',
89+
90+
// Setting this option to true will send default PII data to Sentry
91+
// For example, automatic IP address collection on events
92+
sendDefaultPii: true,
93+
94+
// Set release version to match application version
95+
release: `xcodebuildmcp@${version}`,
96+
97+
// Set environment based on NODE_ENV
98+
environment: process.env.NODE_ENV ?? 'development',
99+
100+
// Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring
101+
// We recommend adjusting this value in production
102+
tracesSampleRate: 1.0,
103+
});
113104
}
114105

115-
const envVars = getEnvironmentVariables();
116-
tags.env_XCODEBUILDMCP_DEBUG = envVars['XCODEBUILDMCP_DEBUG'] ?? 'false';
117-
tags.env_XCODEMAKE_ENABLED = envVars['INCREMENTAL_BUILDS_ENABLED'] ?? 'false';
106+
// Add additional context that might be helpful for debugging (only in production)
107+
if (!isTestEnvironment) {
108+
const tags: Record<string, string> = {
109+
nodeVersion: process.version,
110+
platform: process.platform,
111+
arch: process.arch,
112+
};
113+
114+
// Only add Xcode Info if it's available
115+
const xcodeInfo = getXcodeInfo();
116+
if (!xcodeInfo.error) {
117+
tags.xcodeVersion = xcodeInfo.version;
118+
tags.xcodePath = xcodeInfo.path;
119+
} else {
120+
tags.xcodeVersion = 'Unknown';
121+
tags.xcodePath = 'Unknown';
122+
}
118123

119-
const miseAvailable = checkBinaryAvailability('mise');
120-
tags.miseAvailable = miseAvailable.available ? 'true' : 'false';
121-
tags.miseVersion = miseAvailable.version ?? 'Unknown';
124+
const envVars = getEnvironmentVariables();
125+
tags.env_XCODEBUILDMCP_DEBUG = envVars['XCODEBUILDMCP_DEBUG'] ?? 'false';
126+
tags.env_XCODEMAKE_ENABLED = envVars['INCREMENTAL_BUILDS_ENABLED'] ?? 'false';
122127

123-
const axeAvailable = checkBinaryAvailability('axe');
124-
tags.axeAvailable = axeAvailable.available ? 'true' : 'false';
125-
tags.axeVersion = axeAvailable.version ?? 'Unknown';
128+
const miseAvailable = checkBinaryAvailability('mise');
129+
tags.miseAvailable = miseAvailable.available ? 'true' : 'false';
130+
tags.miseVersion = miseAvailable.version ?? 'Unknown';
126131

127-
Sentry.setTags(tags);
132+
const axeAvailable = checkBinaryAvailability('axe');
133+
tags.axeAvailable = axeAvailable.available ? 'true' : 'false';
134+
tags.axeVersion = axeAvailable.version ?? 'Unknown';
135+
136+
Sentry.setTags(tags);
137+
}

0 commit comments

Comments
 (0)