Repository: microsoft/ApplicationInsights-node.js
Pull request #593 (2020-02-12 22:16:53)

chore: fix misc typos
- Fix misc agent typos


Patch:
diff --git a/Bootstrap/Default.ts b/Bootstrap/Default.ts
index db82d2c..eeab865 100644
--- a/Bootstrap/Default.ts
+++ b/Bootstrap/Default.ts
@@ -11,7 +11,7 @@ let _prefix = "ad_"; // App Services, Default
 let _statusLogger: StatusLogger = new ConsoleStatusLogger();
 
 // Env var local constants
-const ENV_extensionVersion = "APPLICATIONINSIGHTS_EXTENSION_VERSION";
+const ENV_extensionVersion = "ApplicationInsightsAgent_EXTENSION_VERSION";
 const _setupString = process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || process.env.APPINSIGHTS_INSTRUMENTATION_KEY;
 const _extensionEnabled = process.env[ENV_extensionVersion] && process.env[ENV_extensionVersion] !== "disabled";
 
diff --git a/Bootstrap/Helpers.ts b/Bootstrap/Helpers.ts
index dcc53fd..b40486d 100644
--- a/Bootstrap/Helpers.ts
+++ b/Bootstrap/Helpers.ts
@@ -3,7 +3,14 @@ import { AgentLogger } from "./DataModel";
 export function sdkAlreadyExists(_logger: AgentLogger = console): boolean {
     try {
         // appInstance should either resolve to user SDK or crash. If it resolves to attach SDK, user probably modified their NODE_PATH
-        const appInstance = require.resolve("applicationinsights"); // assumes that the cwd is near user's package.json
+        let appInstance;
+        try {
+            // Node 8.9+
+            appInstance = (require.resolve as any)("applicationinsights", { paths: process.cwd() });
+        } catch (e) {
+            // Node <8.9
+            appInstance = require.resolve(process.cwd() + "/node_modules/applicationinsights");
+        }
         const attachInstance = require.resolve("../applicationinsights");
         if (appInstance !== attachInstance) {
             _logger.log(
diff --git a/Bootstrap/StatusLogger.ts b/Bootstrap/StatusLogger.ts
index 73e4c23..0dd79db 100644
--- a/Bootstrap/StatusLogger.ts
+++ b/Bootstrap/StatusLogger.ts
@@ -14,7 +14,7 @@ export interface StatusContract {
 }
 
 function readPackageVersion() {
-    let packageJsonPath = path.resolve(__dirname, "../package.json");
+    let packageJsonPath = path.resolve(__dirname, "../../package.json");
     try {
         let packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
         if (packageJson && typeof packageJson.version === "string") {


Test patch:
diff --git a/Tests/Bootstrap/Default.spec.ts b/Tests/Bootstrap/Default.spec.ts
index c9bd13f..df53293 100644
--- a/Tests/Bootstrap/Default.spec.ts
+++ b/Tests/Bootstrap/Default.spec.ts
@@ -37,8 +37,8 @@ describe("#setupAndStart()", () => {
     it("should setup and start the SDK", () => {
         // Setup env vars before requiring loader
         const logger = new LoggerSpy();
-        const origEnv = process.env.APPLICATIONINSIGHTS_EXTENSION_VERSION;
-        process.env.APPLICATIONINSIGHTS_EXTENSION_VERSION = "~2";
+        const origEnv = process.env.ApplicationInsightsAgent_EXTENSION_VERSION;
+        process.env.ApplicationInsightsAgent_EXTENSION_VERSION = "~2";
         const alreadyExistsStub = sinon.stub(Helpers, "sdkAlreadyExists", () => false);
 
         // Test
@@ -50,7 +50,7 @@ describe("#setupAndStart()", () => {
         // Cleanup
         alreadyExistsStub.restore();
         instance.dispose();
-        process.env.APPLICATIONINSIGHTS_EXTENSION_VERSION = origEnv;
+        process.env.ApplicationInsightsAgent_EXTENSION_VERSION = origEnv;
 
         // start was called once
         assert.equal(startSpy.callCount, 1);
@@ -63,8 +63,8 @@ describe("#setupAndStart()", () => {
     it("should not setup and start the SDK if it has been disabled", () => {
         // Setup
         const logger = new LoggerSpy();
-        const origEnv = process.env.APPLICATIONINSIGHTS_EXTENSION_VERSION;
-        process.env.APPLICATIONINSIGHTS_EXTENSION_VERSION = "disabled";
+        const origEnv = process.env.ApplicationInsightsAgent_EXTENSION_VERSION;
+        process.env.ApplicationInsightsAgent_EXTENSION_VERSION = "disabled";
         const alreadyExistsStub = sinon.stub(Helpers, "sdkAlreadyExists", () => false);
 
         // Test
@@ -75,7 +75,7 @@ describe("#setupAndStart()", () => {
 
         // Cleanup
         alreadyExistsStub.restore();
-        process.env.APPLICATIONINSIGHTS_EXTENSION_VERSION = origEnv;
+        process.env.ApplicationInsightsAgent_EXTENSION_VERSION = origEnv;
 
         // start was never called
         assert.equal(startSpy.callCount, 0);
@@ -88,10 +88,10 @@ describe("#setupAndStart()", () => {
     it("should not setup and start the SDK if no setupString is provided", () => {
         // Setup
         const logger = new LoggerSpy();
-        const origEnv = process.env.APPLICATIONINSIGHTS_EXTENSION_VERSION;
+        const origEnv = process.env.ApplicationInsightsAgent_EXTENSION_VERSION;
         const origIkey = process.env.APPINSIGHTS_INSTRUMENTATION_KEY;
         const origCs = process.env.APPLICATIONINSIGHTS_CONNECTION_STRING;
-        process.env.APPLICATIONINSIGHTS_EXTENSION_VERSION = "~2";
+        process.env.ApplicationInsightsAgent_EXTENSION_VERSION = "~2";
         delete process.env.APPINSIGHTS_INSTRUMENTATION_KEY;
         delete process.env.APPLICATIONINSIGHTS_CONNECTION_STRING;
         const alreadyExistsStub = sinon.stub(Helpers, "sdkAlreadyExists", () => false);
@@ -104,7 +104,7 @@ describe("#setupAndStart()", () => {
 
         // Cleanup
         alreadyExistsStub.restore();
-        process.env.APPLICATIONINSIGHTS_EXTENSION_VERSION = origEnv;
+        process.env.ApplicationInsightsAgent_EXTENSION_VERSION = origEnv;
 
         // start was never called
         assert.equal(startSpy.callCount, 0);